阿晨1998
this在JavaScript中是非常特殊和强大的。它可以代表任何东西。我报道了其中的一些这里和这里,但确实值得找到一本关于JavaScript的好教程,并花一些时间来学习它。让我们先看一下jQuery的使用情况,然后再用JavaScript(稍微一点)来讨论它。在jQuery中,特别是在用jQuery编写的代码中,this 通常引用正在调用的函数的主题DOM元素(例如,在事件回调中)。示例jQuery事件回调(什么this覆盖在这个.bind博士):$("div").click(function() {
// Here, `this` will be the DOM element for the div that was clicked,
// so you could (for instance) set its foreground color:
this.style.color = "red";
// You'll frequently see $(this) used to wrap a jQuery object around the
// element, because jQuery makes lots of things a lot simpler. You might
// hide the element, for example:
$(this).hide();});类似地,对当前jQuery选择器匹配的所有元素执行操作的各种jQuery函数也可以选择性地接受一个函数,并且当该函数被调用时,this也是问题中的DOM元素-例如,html函数允许这样做:// Find all divs inside the `foo` element, and set// their content to their CSS class name(s)
// (Okay, so it's a hokey example)$("#foo div").html(function() {
return this.className;});jQuery使用的另一个地方this在回调中jQuery.each:var a = ["one", "two", "three"];jQuery.each(a, function() {
alert(this);});.会提醒“一”,然后“二”,然后“三”如您所见,这是一个完全不同的用法this.(令人困惑的是,jQuery有两个叫做each,它位于jQuery/$函数本身上,并且总是以这种方式调用[jQuery.each(...)或$.each(...),在jQuery上有一个不同的实例[对象]而不是jQuery/$函数本身。这是文件对于另一个,我不讨论这个答案中的另一个,因为它使用this同样的方式html事件回调功能,我想向您展示一个异类使用this)泛指JavaScriptthis指的是物体。 最新情况:从ES5的严格模式来看,这不再是真的,this可能有任何价值。价值this在任何给定的函数调用中,由函数是如何调用的(没有定义函数的地方,如C#或Java等语言)。最常见的方法this调用函数时,通过对象上的属性调用该函数:var obj = {};obj.foo = function() {
alert(this.firstName);};obj.firstName = "Fred";obj.foo(); // alerts "Fred"因为我们打电话给foo通过一项财产obj, this被设置为obj在通话期间。但别以为foo以任何方式与.obj,这工作得很好:var obj = {};obj.foo = function() {
alert(this.firstName);};obj.firstName = "Fred";obj.foo(); // alerts "Fred"var differentObj = {};
differentObj.firstName = "Barney";differentObj.bar = obj.foo; // Not *calling* it, just getting a reference to itdifferentObj.bar();
// alerts "Barney"事实上,foo不是本质上与任何对象:var f = obj.foo; // Not *calling* it, just getting a reference to itf(); // Probably alerts "undefined"在那里,因为我们没有打电话f通过对象属性,this没有明确设定。什么时候this未显式设置,则默认为全局对象(window在浏览器中)。window可能没有财产firstName所以我们的警戒线是“未定的”。还有其他方法来调用函数并设置this是:通过使用函数的.call和.apply职能:function foo(arg1, arg2) {
alert(this.firstName);
alert(arg1);
alert(arg2);}var obj = {firstName: "Wilma"};foo.call(obj, 42, 27); // alerts "Wilma", "42", and "27"call集this给出第一个参数,然后将任何其他参数传递给它调用的函数。apply执行完全相同的操作,但是将函数的参数作为数组而不是单独给它:var obj = {firstName: "Wilma"};var a = [42, 27];foo.apply(obj, a); // alerts "Wilma", "42", and "27"//
^-- Note this is one argument, an array of arguments for `foo`不过,还有很多事情要探讨this用JavaScript写的。这个概念很强大,如果你习惯了其他语言的做法(如果你习惯了其他语言的话),那就有点欺骗性了,值得知道。下面是一些例子this不指ES5的严格模式中的对象:(function() {
"use strict"; // Strict mode
test("direct");
test.call(5, "with 5");
test.call(true, "with true");
test.call("hi", "with 'hi'");
function test(msg) {
console.log("[Strict] " + msg + "; typeof this = " + typeof this);
}})();产出:[Strict] direct; typeof this = undefined
[Strict] with 5; typeof this = number
[Strict] with true; typeof this = boolean
[Strict] with 'hi'; typeof this = string而在松散模式下,所有这些都会说typeof this = object; 活拷贝.