使用“var that = this”了解Javascript范围 [英] Understanding Javascript scope with "var that = this"
问题描述
假设我在对象中有以下属性方法:
Say I have the following property method in an object:
onReady: function FlashUpload_onReady()
{
Alfresco.util.Ajax.jsonGet({
url: Alfresco.constants.PROXY_URI + "org/app/classification",
successCallback: {
fn: function (o) {
var classButtonMenu = [],
menuLabel, that = this;
var selectButtonClick = function (p_sType, p_aArgs, p_oItem) {
var sText = p_oItem.cfg.getProperty("text");
that.classificationSelectButton.set("label", sText);
};
for (var i in o.json.items) {
classButtonMenu.push({
text: o.json.items[i].classification,
value: o.json.items[i].filename,
onClick: {fn: selectButtonClick}
});
}
this.classificationSelectButton = new YAHOO.widget.Button({
id: this.id + "-appClassification",
type: "menu",
label: classButtonMenu[0].text,
name: "appClassification",
menu: classButtonMenu,
container: this.id + "-appClassificationSection-div"
});
},
scope: this
},
failureMessage: "Failed to retrieve classifications!"
});
在 selectButtonClick中我花了一些猜测工作
函数,我需要引用那个
而不是这个
才能访问 this.classificationSelectButton
(否则它出现 undefined
),但我不确定为什么我不能使用此
。我最好的猜测是,在 new YAHOO.widget.Button
中引用的整个对象中的任何属性都会在调用构造函数后以某种方式失去作用域。
It took me some guess work to figure out that in the selectButtonClick
function that I needed to reference that
instead of this
in order to gain access to this.classificationSelectButton
(otherwise it comes up undefined
), but I'm uncertain as to why I can't use this
. My best guess is that any properties in the overall object that gets referenced within new YAHOO.widget.Button
somehow looses scope once the constructor function is called.
有人可以解释为什么我必须引用 classificationSelectButton
与 var that = this
而不是只调用`this.classificationSelectButton'?
Could someone please explain why it is that I have to reference classificationSelectButton
with var that = this
instead of just calling `this.classificationSelectButton'?
推荐答案
最重要的是要理解一个函数对象没有固定的此
值 - 此
的值会根据具体情况而变化函数被调用。我们说一个函数是用一些特定的这个
值调用的 - 这个
值是在调用时确定的,而不是定义时间。
The most important thing to understand is that a function object does not have a fixed this
value -- the value of this
changes depending on how the function is called. We say that a function is invoked with some a particular this
value -- the this
value is determined at invocation time, not definition time.
- 如果函数被称为原始函数(例如,只需执行
someFunc()
),此
将是全局对象(窗口
在浏览器中)(或undefined
如果函数在严格模式下运行)。 - 如果它被作为方法调用对象,
此
将成为调用对象。 - 如果使用
致电
或apply
,此
被指定为调用的第一个参数
或apply
。 - 如果它被称为e vent listener(就像在这里一样),
这个
将是该事件的目标元素。 - 如果它被调用作为
new
的构造函数,这个
将是一个新创建的对象,其原型设置为原型
构造函数的属性。 - 如果函数是
bind
操作,该函数将永远拥有此
设置为生成它的bind
调用的第一个参数。 (这是函数没有固定此
规则的唯一例外 - 由bind
实际上做有一个不可变的这个
。)
- If the function is called as a "raw" function (e.g., just do
someFunc()
),this
will be the global object (window
in a browser) (orundefined
if the function runs in strict mode). - If it is called as a method on an object,
this
will be the calling object. - If you call a function with
call
orapply
,this
is specified as the first argument tocall
orapply
. - If it is called as an event listener (as it is here),
this
will be the element that is the target of the event. - If it is called as a constructor with
new
,this
will be a newly-created object whose prototype is set to theprototype
property of the constructor function. - If the function is the result of a
bind
operation, the function will always and forever havethis
set to the first argument of thebind
call that produced it. (This is the single exception to the "functions don't have a fixedthis
" rule -- functions produced bybind
actually do have an immutablethis
.)
使用 var that = this;
是一种在函数定义时间<存储此
值的方法/ em>(而不是函数执行时间,当时,这个
可以是任何东西,具体取决于函数的调用方式)。这里的解决方案是将这个
的外部值存储在一个变量中(传统上称为
或 self
)包含在新定义的函数的范围内,因为新定义的函数可以访问在其外部作用域中定义的变量。
Using var that = this;
is a way to store the this
value at function definition time (rather than function execution time, when this
could be anything, depending on how the function was invoked). The solution here is to store the outer value of this
in a variable (traditionally called that
or self
) which is included in the scope of the newly-defined function, because newly-defined functions have access to variables defined in their outer scope.
这篇关于使用“var that = this”了解Javascript范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!