为了账号安全,请及时绑定邮箱和手机立即绑定

Pet.call(this, words) 这句话中的this该怎么理解?

    function Pet(words){        
    this.words = words;        
    this.speak = function () {
            console.log( this.words)
        }
    }    
   function Dog(words){        //Pet.call(this, words); //结果: Wang
       Pet.apply(this, arguments); //结果: Wang    }    var dog = new Dog('Wang');
    dog.speak();
Pet.call(this, words)    这句话中的this该怎么理解?


正在回答

1 回答

function Pet(words)是Pet类型的构造函数,Pet.call(this, words)就是用传递的this值,也就是Dog类型,来替换Pet构造函数中的this,使Dog类型的构造函数继承了Pet类型的构造函数,就可以获得其中的words属性和speak函数,所以Dog实例化出的dog对象可以使用Pet类型上的speak函数

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

Pet.call(this, words) 这句话中的this该怎么理解?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信