使用“var that = this”了解Javascript范围 [英] Understanding Javascript scope with "var that = this"

查看:155
本文介绍了使用“var that = this”了解Javascript范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在对象中有以下属性方法:

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) (or undefined 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 or apply, this is specified as the first argument to call or apply.
  • 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 the prototype property of the constructor function.
  • If the function is the result of a bind operation, the function will always and forever have this set to the first argument of the bind call that produced it. (This is the single exception to the "functions don't have a fixed this" rule -- functions produced by bind actually do have an immutable this.)

使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆