3 回答

TA贡献2037条经验 获得超6个赞
this
在jQuery中,特别是
this
通常
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();});
this
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;});
this
jQuery.each
:
var a = ["one", "two", "three"];jQuery.each(a, function() { alert(this);});
this
.
each
jQuery.each(...)
$.each(...)
this
html
this
泛指JavaScript
this
this
this
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
this
(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
; 活拷贝.

TA贡献1876条经验 获得超5个赞
这个关键字
在JavaScript中,被称为“拥有”JavaScript代码的对象。
注意,这不是一个变量。这是一个关键字。您不能更改此值。
添加回答
举报