Javascript &引用;这";闭包函数内部
这是一个封闭的内部。 需要注意的是,闭包不能使用this关键字访问外部函数的this变量,因为this变量只能由函数本身访问,而不能由内部函数访问 例如: 我的问题是:为什么下面的函数引用jquery的按钮对象而不是窗口对象。毕竟,回调函数(某个函数)仍然在另一个函数(单击)中 $(“按钮”)。单击(某些功能) 此外,我还研究了另一个与此类似的问题,但我仍然一点也不明白 我的问题是:为什么下面的函数引用jquery的按钮对象而不是窗口对象 因为jQuery调用处理程序,通过显式地设置Javascript &引用;这";闭包函数内部,javascript,Javascript,这是一个封闭的内部。 需要注意的是,闭包不能使用this关键字访问外部函数的this变量,因为this变量只能由函数本身访问,而不能由内部函数访问 例如: 我的问题是:为什么下面的函数引用jquery的按钮对象而不是窗口对象。毕竟,回调函数(某个函数)仍然在另一个函数(单击)中 $(“按钮”)。单击(某些功能) 此外,我还研究了另一个与此类似的问题,但我仍然一点也不明白 我的问题是:为什么下面的函数引用jquery的按钮对象而不是窗口对象 因为jQuery调用处理程序,通过显式地设置this的含
this
的含义(也可能是,我必须查看jQuery源代码)
下面是一个使用调用的简单示例:
function foo() {
console.log("this.answer = " + this.answer);
}
var obj = {answer: "42"};
foo.call(obj); // The first argument is used as `this` during the call
这将产生
this.answer = 42
this.answer=42您有权使用“this”关键字,该关键字引用了当前正在执行的方法所调用的对象。因此,在第一个示例中,clickHandler()
函数将引用用户对象
现在在jQuery中,当您在回调函数中时,“this”指的是“DOM”元素。据我所知,原因是jQuery从其内部“jQuery”代码返回一个对象,该代码使用call()和apply()维护对上下文中作为“DOM”元素的元素的引用。我相信我会坚持下去。这样做还允许您完成动作链接,如(“按钮”)。例如,单击(somefunction).fadeIn()
如果创建自己的jquery函数,例如$.fn.somefunction=function(){…}
,则此时会引用jquery对象
可能有更好的理由来实现这一点,但我使用call()
快速更改了您的代码,以使is引用您的用户对象
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]
....
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)
}
}
另一件事是在Javascript中,forEach
函数接受第二个参数,该参数将被用作“this”的引用对象,因此您可以用另一种方式来完成。现在它指的是用户对象
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]
....
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网站上的解释,这里有一个链接。
因此,基本上我们一直使用call或apply,直到我们将“this”设置为我们希望它引用的内容。例如,$(“按钮”)。单击(object.somefunction.call(obj))对吗?@tomatos:不,该代码将立即调用object.somefunction
,并将其结果传递到$(“#按钮”)。单击()。我想说的是,这个
引用了单击
回调中的DOM元素(不是jQuery对象),因为jQuery故意确保它这样做。在内部,jQuery通过call
(或apply
)调用您的处理程序来实现这一点。我试图在object.somefunction.call(obj))
中将this
设置为obj
,但我破坏了与它的讨论。我知道$(this)指的是jquery对象,而this
则返回到javascriptthis
。但是,在click中的回调函数中,this
指的是$(“button”)jquery对象,因为通常this
指的是启动包含this
的函数的对象。因此,根据您所说的,click中的回调函数中的this
应该指的是启动对象$(“button”)jquery对象,但jquery使其有意引用DOM元素(窗口对象)?@Tomotos:不,将回调传递到函数中没有任何内在的东西,该函数以任何方式定义调用回调时该
将是什么。这完全由您将回调传递到的API定义。(回复您的电子邮件:是的,因此会通知他人回复。)