何时在 JavaScript 中使用 self

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3309516/
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-08-23 04:06:26  来源:igfitidea点击:

When to use self in JavaScript

javascript

提问by Konrad

I've noticed that calls like setTimeout()work either as :

我注意到调用之类的setTimeout()工作要么是:

self.keyword()

self.keyword()

or just on their own e.g. keyword().

或仅靠他们自己,例如keyword()

What is the different between the two calls?

这两个调用有什么不同?

采纳答案by Nick Craver

selfcanrefer to the window object, but typically that's not the case here. You'll see this commonly above that setTimeout():

self可以参照窗口对象,但通常是这里的情况并非如此。您通常会在上面看到这一点setTimeout()

var self = this;

They're keeping a reference to the current object, so later when you call self.keyword()you're calling that method on that object, not any other.

他们保持对当前对象的引用,所以稍后当您调用时,self.keyword()您将在该对象上调用该方法,而不是其他任何方法。

Say you have for example images in the page you wanted to rotate every 2 seconds...you'd want each of those 3 timers to refer to their ownmethods. If they use thisdirectly, it would (most of the time) refer to windowand not the current object, whereas passing another variable in maintains the current reference.

假设您希望每 2 秒旋转一次页面中的图像……您希望这 3 个计时器中的每一个都引用它们自己的方法。如果他们this直接使用,它会(大部分时间)引用window而不是当前对象,而传递另一个变量会维护当前引用。

回答by Felix Kling

It works with setTimeoutbecause of two conditions in the browser:

它适用setTimeout于浏览器中的两个条件:

  • All global variables are properties of the windowobject. That means that windowhas a property setTimeout(window.setTimeout).
  • The windowobject has a property called selfthat points to itself.
  • 所有全局变量都是window对象的属性。这意味着它window有一个属性setTimeout( window.setTimeout)。
  • window对象有一个称为self指向自身的属性。

As you can access the properties of windowwithout explicitly writing window(that is what makes the global variables global), both calls work: setTimeout()will look up the property setTimeout()on the window object. self.setTimeout()will look up the property selfon the windowobject, which is the window object itself.

由于您可以在window不显式写入的情况下访问 的属性window(这就是使全局变量成为全局变量的原因),因此两个调用都有效:setTimeout()setTimeout()在 window 对象上查找属性。self.setTimeout()将查找对象self上的window属性,即窗口对象本身。

So if you call self.setTimeout()it is the same as window.self.setTimeout()which is the same as window.setTimeout()which again is the same as setTimeout().

因此,如果您称它为self.setTimeout()相同window.self.setTimeout()于相同window.setTimeout()于相同于相同于setTimeout().

Note: This only works if there is no variable selfdefined in the current scope that shadows the global self.

注意:这仅self在当前作用域中没有定义影响 global 的变量时才有效self

This works with anysymbol (meaning variable or function) defined in the global scope. You can test it yourself:

这适用于在全局范围内定义的任何符号(表示变量或函数)。你可以自己测试一下:

alert(window.self);

and

alert(self);

should both alert

都应该警觉

[object Window]

回答by Hitesh.Aneja

Every property and method on windowobject can be called with or without 'window.'.

window对象上的每个属性和方法都可以使用或不使用“window.”来调用。

and

self is a read-only property on window object that returns the window itself (MDN)

self 是 window 对象上的只读属性,它返回窗口本身 (MDN)

so

所以

setTimeout()

setTimeout()

window.setTimeout()

window.setTimeout()

window.self.setTimeout()

window.self.setTimeout()

self.setTimeout()

self.setTimeout()

are all same thing.

都是一样的东西。

The main advantage of doing self.setTimeout()instead of window.setTimeout()or any other way is that, if you run some code that calls window.setTimeout()inside WebWorker, it will fail but the self.setTimeout()will work both in web workers and the browser context. So if you are writing a library that should work both on main window's scope and the web worker, we should prefer using self.

self.setTimeout()代替window.setTimeout()或任何其他方式的主要优点是,如果您运行一些window.setTimeout()在 WebWorker 内部调用的代码,它会失败,但它会self.setTimeout()在 Web Worker 和浏览器上下文中工作。因此,如果您正在编写一个可以在主窗口范围和 web worker 上工作的库,我们应该更喜欢使用self.

selfalways refer to the GlobalScope which in case of browser mode is windowand inside web workers is `WorkerGlobalScope'

self始终引用 GlobalScope,在浏览器模式下window,Web 工作者内部为“WorkerGlobalScope”