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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:19:03  来源:igfitidea点击:

Difference between this and self in JavaScript

javascript

提问by Ben Nelson

Everyone is aware of thisin javascript, but there are also instances of selfencountered in the wild, such as here

this在javascript中大家都知道,但也有self在野外遇到的实例,比如这里

So, what is the difference between thisand selfin JavaScript?

那么,JavaScript 中的this和 有什么区别self

回答by Paul S.

Unless set elsewhere, the value of selfis windowbecause JavaScriptlets you access any property xof windowas simply x, instead of window.x. Therefore, selfis really window.self, which is different to this.

除有其他地方的价值selfwindow因为的JavaScript可以访问任何财产xwindow简单地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, thisdefaults 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, thiswill refer to that context, but selfwill still be window.

如果您在不同的上下文中使用函数,this将引用该上下文,但self仍将是window.

// invoke foo with context {}
foo.call({}); // true false false

You can find window.selfdefined 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 thisfurther:

虽然我迟到了,但我遇到了一个例子,它也有助于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, thisin the inner function would refer to the global window object; whereas, as of ECMA 5, thisin 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)的方式引用全局范围。