首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >闭包函数中的"this“

闭包函数中的"this“
EN

Stack Overflow用户
提问于 2013-11-27 12:20:02
回答 1查看 465关注 0票数 0

这是一个封闭的地方。需要注意的是,闭包不能使用this关键字访问外部函数的这个变量,因为这个变量只能由函数本身访问,而不是通过内部函数访问。

例如:

var user ={锦标赛:“主赛”,数据:{name:“T.Woods”,年龄:37},{name:“P.Mickelson”,age:43},clickHandler:function () { // this.data在这里的用法很好,因为"this“指的是用户对象,而数据是用户对象上的属性。this.data.forEach (函数(person) { //但在匿名函数中(我们传递给forEach方法),"this“不再指用户对象。//此内部函数无法访问外部函数的" This“console.log (”这指的是什么?“+ this);//对象窗口console.log (person.name +”正在“播放”+this.tournament“);// T.伍兹在未定义的// P. Mickelson在玩未定义}}user.clickHandler()} user.clickHandler();//这指的是什么?目标窗口

我的问题是:为什么下面的函数引用jquery的按钮对象而不是窗口对象。毕竟,回调函数(某些函数)仍然在另一个函数中(单击)。

$(“按钮”).click(一些函数);

此外,我看了另一个类似的问题,但我仍然没有更聪明。"this" keyword inside closure

EN

回答 1

Stack Overflow用户

发布于 2013-11-27 13:10:43

您可以正确地看到'this‘关键字,它引用了调用当前正在执行的方法的对象。因此,在第一个示例中,clickHandler()函数将引用user对象。

现在,就jQuery而言,当您在回调函数‘’中时,这是指'DOM‘元素。我所理解的原因是,jQuery从其内部'jQuery‘代码中返回一个对象,该代码使用call()和apply()维护对上下文中元素的引用,即'DOM’元素。我相信能维持下去。这样做还允许您完成例如("button").click(somefunction).fadeIn()这样的操作链接。

如果您创建了自己的jquery函数(如$.fn.somefunction = function() {...} ),那么此时这里引用的是一个jQuery对象。

也许有一个更好的理由来完成这个任务,但是我使用call()快速地更改了您的代码,使之引用您的用户对象。

代码语言:javascript
复制
var user = {
    tournament:"The Masters",
    data      :[
        {name:"T. Woods", age:37},
        {name:"P. Mickelson", age:43}
    ],

    clickHandler: function () {
    // the use of this.data here is fine, because "this" refers to the user object, 
    // and data is a property on the user object.

    this.data.forEach (function (person) {
        // But here inside the anonymous function (that we pass to the forEach method),
        //"this" no longer refers to the user object.
        // This inner function cannot access the outer function's "this"

        //Use call to make this refer to your user object
        that = Object.call(this, user);

         console.log ("What is This referring to? " + that); //[object Object]

         console.log (person.name + " is playing at " + that.tournament);
         // T. Woods is playing at undefined
         // P. Mickelson is playing at undefined
        })
     }

    }

user.clickHandler(); // What is This referring to? [object Object]

另一件事是,在Javascript中,forEach函数接受第二个参数,该参数将用作引用“this”的对象,因此您可以使用另一种方法。现在这是指用户对象。

代码语言:javascript
复制
....

this.data.forEach (function (person) {
    // But here inside the anonymous function (that we pass to the forEach method),
    //"this" no longer refers to the user object.
    // This inner function cannot access the outer function's "this"

    //Use call to make this refer to your user object

     console.log ("What is This referring to? " + this); //[object Object]

     console.log (person.name + " is playing at " + this.tournament);
     // T. Woods is playing at Masters
     // P. Mickelson is playing at Masters
     //pass user as the object the second parameter
    }, user)
 }

}

查看jquery站点上对此的解释,这里有一个链接.

http://learn.jquery.com/javascript-101/this-keyword/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20242351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档