Discuss / JavaScript / 有个问题

有个问题

Topic source

jaychang87

#1 Created at ... [Delete] [Delete and Lock User]
function Cat(name) {
    this.name = name;
}
Cat.prototype.say = function(){
    return "Hello, "+this.name+"!";
}

有点不解的时Cat.prototype.say方法里的this指向的是kitty还是Cat.prototype

jaychang87

#2 Created at ... [Delete] [Delete and Lock User]

指向的是kitty

廖雪峰

#3 Created at ... [Delete] [Delete and Lock User]

this是当前对象,也就是:

var kitty = new Cat('Kitty');
kitty.say(); // this指向变量kitty

  • 1

Reply