直面坑爹的前端面试

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cometwo/article/details/52644246

参考:http://www.zhufengpeixun.cn/JavaScriptmianshiti/2014-02-28/271.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript">
            function A(){
                this.say=function(){
                    console.log("我是1");
                }
            }
            function B(){
                this.say=function(){
                    console.log("我是2");
                }
            }
            var a=new A();
            var b=new B();
            a.say.call(b);    //我是1
        </script>
    </head>
    <body>
    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript">
            function A(){
                this.name='SB';
                this.say=function(){
                    console.log("我是1");
                }
            }
            function B(){
                A.call(this);
                this.say=function(){
                    console.log("我是2");
                }
            }
            var a=new A();
            var b=new B();
            console.log(b.name);  //SB
            b.say();         //我是2
            a.say.call(b);    //我是1
        </script>
    </head>
    <body>
    </body>
</html>

猜你喜欢

转载自blog.csdn.net/cometwo/article/details/52644246