【发布时间】:2013-02-01 14:22:19
【问题描述】:
谁能告诉 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;}
}
【问题讨论】:
-
在这两种情况下,它都指向
B实例,因此您可以在其上调用a/x。 -
this在运行时而不是在解析/定义时确定。仅通过查看定义,您无法判断this将指代什么。这完全取决于如何调用该函数。
标签: javascript web function-prototypes