[英]JavaScript “this” in a function block
this
refers to the object it belongs to, for example: this
是指它所属的对象,例如:
var someObj = {
logObj : function() { return this }
}
obj.logObj() => someObj
and a function is an object. 函数是对象。 But then why does this
in a function refer to the window, not the function? 但是,为什么在函数中this
引用窗口而不是函数呢? For example, 例如,
function someFunc() { return this }
won't return someFunc()
, but window
. 不会return someFunc()
,但是会return someFunc()
window
。
It's true, a function is an object. 的确,函数是对象。 However, the statements inside the function are not called with this
set to the function itself. 然而,在函数内部的语句不调用this
设置函数本身。 That would keep this
from referencing the object that it was called on, and therefore eliminate much of the usefulness of this
. 这样可以避免this
引用被调用的对象,从而消除了this
许多有用性。 There are ways to accomplish what you seek though. 虽然有一些方法可以实现您的目标。
"When a function is called as a method of an object, the object is passed to the function as its this value." “当将函数作为对象的方法调用时,该对象将作为此值传递给函数。” ECMAScript Specification 4.3.31 ECMAScript规范4.3.31
Functions are not called as methods of themselves. 函数不称为自身的方法。 Functions not executed as methods of an object are called as methods of the global object (or undefined
if in "strict" mode). 未作为对象的方法执行的函数称为全局对象的方法(如果处于“严格”模式,则undefined
)。
function test() { console.log(this == window); } var obj = {'test': test}; test(); window.test(); obj.test();
If you really want for the this
in a function to refer to itself, then you will have to add the function as a property of itself, or use a function such as apply
, call
, or bind
which have a thisArg
. 如果您确实希望函数中的this
引用自身,则必须将该函数添加为自身的属性,或者使用具有thisArg
的函数(例如apply
, call
或bind
。
function test() { console.log(this) }; test.test = test; test.test(); test.call(test); test.apply(test); test.bind(test)();
Context is most often determined by how a function is invoked. 上下文通常由函数的调用方式决定。 When a function is called as a method of an object, this is set to the object the method is called on: 当将函数作为对象的方法调用时,会将其设置为在其上调用该方法的对象:
var obj = {
foo: function(){
alert(this === obj);
}
};
obj.foo(); // true
The same principle applies when invoking a function with the new operator to create an instance of an object. 使用new运算符调用函数以创建对象的实例时,适用相同的原理。 When invoked in this manner, the value of this within the scope of the function will be set to the newly created instance: 以这种方式调用时,在函数范围内的this值将设置为新创建的实例:
function foo(){
alert(this);
}
foo() // window
new foo() // foo
When called as an unbound function, this will default to the global context or window object in the browser. 当作为未绑定函数调用时,它将默认为浏览器中的全局上下文或窗口对象。 However, if the function is executed in strict mode, the context will default to undefined. 但是,如果函数以严格模式执行,则上下文将默认为未定义。
There are at least two ways to create an object - with an object literal, or with an object constructor. 至少有两种方法可以创建对象-使用对象文字或对象构造函数。
Only when you use the latter technique will this
refer to the object it occurs within. 只有当你使用后者技术将this
参考它发生内的对象。
This is how to create an object with an object constructor (in your example, a literal was used) : 这是使用对象构造函数创建对象的方法(在您的示例中,使用了文字):
var objConstructor = function () {
this.logObj = function () {return this}
}
var obj1 = new objConstructor()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.