任何人都可以在js中告诉“this”关键字..
我看了一些例子.有一点我无法理解.
A.B=function()
{
this.x(5); // this refers to prototype of A.B
}
A.B.prototype= {
x:function(p)
{ this.a(p); // this refers to prototype of A.B again
// but I expect that this refers to protoype of x ???
},
a:function(p){ return p;}
}
最佳答案 如果你调用一个方法:
a.b.c.d();
那么这是方法内部的a.b.c(除最终函数名之外的所有内容).
如果你调用一个构造函数:
var x = new Something();
然后这是Something()内部的新鲜对象.
这是其他任何地方的全局对象(与浏览器中的窗口相同).
这绝不是原型.这可以有一个原型.
在你的例子中:
A.B = function() {
this.x(5);
}
如果该方法被称为A.B(),那么这是A(它不必是A.B的原型) – 如果该方法被称为新的A.B(),则它是一个新对象.