javascript self 和 window 和有什么不一样?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3638887/
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
What's the difference between self and window?
提问by Milan Babu?kov
I have a JavaScript that deals with with detection whether the page is in frames or not. I used top.frames[] etc. and everything works fine.
我有一个 JavaScript 来处理检测页面是否在框架中。我使用了 top.frames[] 等,一切正常。
In this script I noticed that I can use "window" or "self" interchangeably and everything still works. Is "window" same as "self" when used in HTML page?
在这个脚本中,我注意到我可以交替使用“window”或“self”,但一切仍然有效。在 HTML 页面中使用时,“window”与“self”是否相同?
采纳答案by Ender
From Javascript: The Definitive Guide:
来自Javascript:权威指南:
The Window object defines a number of properties and methods that allow you to manipulate the web browser window. It also defines properties that refer to other important objects, such as the
documentproperty for the Document object. Finally, the Window object has two self-referential properties,windowandself. You can use either global variable to refer directly to the Window object.
Window 对象定义了许多允许您操作 Web 浏览器窗口的属性和方法。它还定义了引用其他重要对象的
document属性,例如Document 对象的属性。最后,Window 对象有两个自引用属性,window和self. 您可以使用任一全局变量直接引用 Window 对象。
In short, both windowand selfare references to the Window object, which is the global object of client-side javascript.
简而言之,window和self都是对Window对象的引用,该对象是客户端javascript的全局对象。
回答by Titus
selfis a read-onlyproperty that can be more flexible than, and sometimes used in favor of, the windowdirectly. This is because self's reference changes depending on the operating context (unlike window.self, which only exists if windowexists). It's also great for comparisons, as others have mentioned.
self是一个只读属性,可以比window直接使用更灵活,有时用于支持。这是因为self的引用根据操作上下文而变化(与 不同window.self,它仅在window存在时才存在)。正如其他人提到的那样,它也非常适合进行比较。
For example, if you use selfinside a Web Worker (which lives in its own background thread), selfwill actually reference WorkerGlobalScope.self. However, if you use selfin a normal browser context, selfwill simply return a reference to Window.self(the one that has document, addEventListener(), and all the other stuff you're used to seeing).
例如,如果你self在一个 Web Worker 内部使用(它存在于它自己的后台线程中),self实际上会引用WorkerGlobalScope.self. 但是,如果你使用self的浏览器中正常情况下,self将简单地返回到基准Window.self(即有一个document,addEventListener()和所有你看惯了其他的东西)。
TL;DR while the .selfin window.selfwill not exist if windowdoesn't exist, using selfon its own will point to Window.selfin a traditional window/browser context, or WorkerGlobalScope.selfin a web worker context.
TL; DR 而.selfinwindow.self如果window不存在则不存在,单独使用self将指向Window.self传统的窗口/浏览器上下文或WorkerGlobalScope.selfWeb Worker 上下文。
As usual, MDN has a great writeup on this subject in their JavaScript docs. :)
像往常一样,MDN在他们的 JavaScript 文档 中有一篇关于这个主题的精彩文章。:)
Side note:The usage of selfhere should not be confused with the common JS pattern of declaring a local variable: var self = thisto maintain a reference to a context after switching.
旁注:selfhere的用法不应与声明局部变量的常见 JS 模式混淆:var self = this在切换后维护对上下文的引用。
You can read more about that here: Getting Out of Binding Situations in JavaScript.
您可以在此处阅读更多相关信息:Getting Out of Binding Situations in JavaScript。
回答by Jesse Dhillon
Here's the explanation and example from the MDN page for window.self:
以下是MDN 页面中window.self的解释和示例:
if (window.parent.frames[0] != window.self) {
// this window is not the first frame in the list
}
window.selfis almost always used in comparisons like in the example above, which finds out if the current window is the first subframe in the parent frameset.
window.self几乎总是在比较中使用,就像上面的例子一样,它确定当前窗口是否是父框架集中的第一个子框架。
Given that nobody is using framesets these days, I think it's okay to consider that there are no useful cases for self. Also, at least in Firefox, testing against windowinstead of window.selfis equivalent.
鉴于现在没有人使用框架集,我认为可以考虑self. 此外,至少在 Firefox 中,对window而不是进行测试window.self是等效的。
回答by davehauser
windowand selfboth refer to the global object of the current web page.
window并且self都指向当前网页的全局对象。
For more info have a look at http://www.howtocreate.co.uk/tutorials/javascript/browserinspecific
有关更多信息,请查看http://www.howtocreate.co.uk/tutorials/javascript/browserinspecific

