【发布时间】:2013-06-11 06:17:29
【问题描述】:
我看到了一些code like this from jqplot library:
$.jqplot.Dragable = function(options) {
// a bunch of code here.
$.extend(true, this, options);
};
这是向库中添加新插件的例程。
this 在$.extend(true, this, options) 中指的是什么?是函数本身还是窗口,还是调用时才会指定?
谢谢!
更新:
这个问题是一个普遍的问题,因为我不明白 js 中的 this 值。经过一番研究,显然:
1.如果使用func()调用,则this将是window或undefined(严格模式)
2. 如果像 foo.funct() 那样调用,this 指的是 foo。
至于这个特定的 jqplot 库,当它被调用时,它实际上是在 $.jqplot 上调用的,其中 jqplot 被添加到 jquery 中。所以this应该参考$.jqplot
【问题讨论】:
-
这里看起来像是
Dragable的一个实例 -
jqplot 库在调用扩展函数之前选择将其设置为什么。在 JS 中不一定是自动的。
-
@ArunPJohny 我对 js oop 很陌生...你是说 Dragable 是构造函数吗?为什么?还是js中的所有函数都可以是构造函数?
-
@Barmar 如果这样调用 $.jqplot.Dragable(optionObj),那么 this == $.jqplot.Dragable?
-
你可以试试
console.log(this),它应该会告诉你它是什么...
标签: javascript this