Javascript javascript中的“自我”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3216428/
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 does "self" mean in javascript?
提问by Nick Van Brunt
I read herethat "self Refers to the current window or form".
我在这里读到“ self 指的是当前窗口或表单”。
Self does not seem to refer to the current form in this case:
在这种情况下,Self 似乎不是指当前的形式:
<form><input type="text" onkeyup="alert(self.foo.value)" name="foo"></form>
However in this case it works (referring to the window):
但是,在这种情况下,它可以工作(指窗口):
<form><input type="text" onkeyup="alert(self.document.forms[0].foo.value)" name="foo"></form>
So when would you use the selfDOM property over just window?
那么你什么时候会使用selfDOM 属性而不是window?
采纳答案by Swift
For all windows, the
selfandwindowproperties of awindowobject are synonyms for the current window, and you can optionally use them to refer to the current window. For example, you can close the current window by calling theclosemethod of eitherwindoworself. You can use these properties to make your code more readable or to disambiguate the property referenceself.statusfrom a form calledstatus.
对于所有窗口,对象的
self和window属性window是当前窗口的同义词,您可以选择使用它们来引用当前窗口。例如,您可以通过调用或的close方法 关闭当前窗口。您可以使用这些属性使您的代码更具可读性或消除名为 的表单中的属性引用的歧义。windowselfself.statusstatus
回答by Garrett
Other replies have pointed out that selfis not going to refer to the FORMand that selfis window. They're right; selfiswindow. Well, except when it isn't. In either IE6 or IE7 (forgot), self.onloadwould not fire, though window.onloadwould.
其他的答复有指出,self不会指FORM那self是window。他们是对的;self是window。好吧,除非它不是。在 IE6 或 IE7(忘记了)中self.onload,虽然不会触发window.onload。
All official versions of IE (and even IE9pr3) have an odd, intransitive implementation of ==with these host objects. Using ==to compare either windowor selfto a node in the document, the result is true.
所有正式版本的 IE(甚至 IE9pr3)都有一个奇怪的、不可传递的实现==与这些宿主对象。使用==要么比较window或self文档中的一个节点,其结果是true。
IE Oddities
IE奇数
alert(self == document.body); // true
alert(document.body == self); // false
alert(window == self); // true
alert(window === self); //false
var b = document.createElement("b");
alert(window == b); // false
alert(window == document.body.appendChild(b)); // true
alert(window == document.body.removeChild(b)); // false
回答by Chadwick
selfis not a reserved keyword or standard type, but has become a defacto standard name when for keeping reference to an object for closures.
self不是保留关键字或标准类型,但在保持对闭包对象的引用时已成为事实上的标准名称。
Here we create a closure to pass to setTimeout(). When that closure is executed, thiswill refer to the global object. To keep a reference to the fooobject doLaterwas originally called on, a var named selfis used. It could be anything but 'self' has meaningful semantics.
这里我们创建了一个闭包来传递给setTimeout()。当执行该闭包时,this将引用全局对象。为了保持对最初调用的foo对象的引用doLater,使用了一个名为 var 的变量self。它可以是任何东西,但“自我”具有有意义的语义。
Foo.prototype.doLater = function() {
var self = this; // current object
window.setTimeout(function() { self.doSomething(); }, 1000);
}
new Foo().doLater();
回答by meder omuraliev
Never, unless I wanted to create a selfvariable in the scope of a function referring to the context for later reference,
从不,除非我想self在引用上下文的函数范围内创建一个变量以供以后参考,
function Constructor() {
var self = this;
}
You should use thisto refer to the element at hand, not self. In global scope though thisis a reference to window, and selfis also window.
您应该使用this来指代手头的元素,而不是self。在全局范围内,虽然this是对 的引用window,但self也是window。
回答by MyUserInStackOverflow
selfrefers to the global scope- If the context is a window it will refer to window.self, while in case of a non-windowed context it will refer to the global scope of that context (e.g. in service worker code, self refers to the worker global scope).
self指的是全局范围- 如果上下文是一个窗口,它将引用 window.self,而在非窗口上下文的情况下,它将引用该上下文的全局范围(例如,在 service worker 代码中,self 指的是工人全球范围)。
self refers to the global scope of a context:
self 指的是上下文的全局范围:
- window context
- worker context (support for multiple workers for the same webapp is being worked on).
- 窗口上下文
- 工作人员上下文(正在为同一个 web 应用程序支持多个工作人员)。
https://developer.mozilla.org/en-US/docs/Web/API/Window/self
https://developer.mozilla.org/en-US/docs/Web/API/Window/self

