JavaScript 中 this 和 self 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16875767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between this and self in JavaScript
提问by Ben Nelson
回答by Paul S.
Unless set elsewhere, the value of self
is window
because JavaScriptlets you access any property x
of window
as simply x
, instead of window.x
. Therefore, self
is really window.self
, which is different to this
.
除有其他地方的价值self
是window
因为的JavaScript可以访问任何财产x
的window
简单地x
代替window.x
。因此,self
是真的window.self
,这与 不同this
。
window.self === window; // true
If you're using a function that is executed in the global scope and is not in strict mode, this
defaults to window
, and therefore
如果您使用的函数在全局范围内执行并且不在严格模式下,则this
默认为window
,因此
function foo() {
console.log(
window.self === window, // is self window?
window.self === this, // is self this?
this === window // is this window?
);
}
foo(); // true true true
If you're using a function in a different context, this
will refer to that context, but self
will still be window
.
如果您在不同的上下文中使用函数,this
将引用该上下文,但self
仍将是window
.
// invoke foo with context {}
foo.call({}); // true false false
You can find window.self
defined in the W3C 2006 working draft for the Window Objecthere.
您可以window.self
在此处找到W3C 2006 工作草案中定义的Window 对象。
回答by Shashank Vivek
Although I am late here but I came across one example which too can be helpful to understand this
further:
虽然我迟到了,但我遇到了一个例子,它也有助于this
进一步理解:
var myObject = {
foo: "bar",
func: function() {
var self = this;
console.log("outer func: this.foo = " + this.foo);
console.log("outer func: self.foo = " + self.foo);
(function() {
console.log("inner func: this.foo = " + this.foo);
console.log("inner func: self.foo = " + self.foo);
}());
}
};
myObject.func();
O/P
开/关
outer func: this.foo = bar
outer func: self.foo = bar
inner func: this.foo = undefined
inner func: self.foo = bar
Prior to ECMA 5,
this
in the inner function would refer to the global window object; whereas, as of ECMA 5,this
in the inner function would be undefined.
在 ECMA 5 之前,
this
在内部函数中会引用全局窗口对象;而从 ECMA 5 开始,this
在内部函数中将是未定义的。
回答by andyhasit
A slight addition to this as people may encounter this in the context of service workers, in which case it means something slightly different.
对此稍作补充,因为人们可能会在 Service Worker 的上下文中遇到这种情况,在这种情况下,它的含义略有不同。
You might see this in a service worker module:
您可能会在 service worker 模块中看到这一点:
self.addEventListener('install', function(e) {
console.log('[ServiceWorker] Install');
});
Here selfrefers to the WorkerGlobalScope, and this is the standard method for setting event listeners.
这里self指的是WorkerGlobalScope,这是设置事件监听器的标准方法。
From Mozilla docs:
来自Mozilla 文档:
By using self, you can refer to the global scope in a way that will work not only in a window context (self will resolve to window.self) but also in a worker context (self will then resolve to WorkerGlobalScope.self).
通过使用 self,您可以以不仅在窗口上下文中(self 将解析为 window.self)而且在工作上下文中(self 将解析为 WorkerGlobalScope.self)的方式引用全局范围。