这个,自我,窗口和窗口之间有什么区别。自己 [英] What is the difference between this, self, window and window.self
问题描述
如果我打开一个空白页面并在javascript控制台中运行以下命令,我会得到相同的结果:
If I open an empty page and I run the following command in javascript console I obtain the same result:
>>> this
DOMWindow
>>> self
DOMWindow
>>> window
DOMWindow
>>> window.self
DOMWindow
他们指的是什么? ...相同的对象或其他什么?
What do they refer to? ...the same object or what else?
推荐答案
window
是对脚本执行的当前浏览器窗口的引用。 window.self
显然是其内部的自引用。由于 self
这里是全局对象窗口
的属性,因此它也可以被访问,就像它是全局变量本身:只需自我
。
window
is the reference to the current browser’s window the script executes in. window.self
is obviously a self-reference of that within itself. And since self
here is a property of the global object window
, it can also be accessed as if it was a "global" variable itself: just self
.
所以最后三个在大多数情况下确实是一样的。
So the last three are under most circumstances indeed the same thing.
这个
然而完全不同:它是一个指向当前范围的变量。如果您在控制台中执行以下代码:
this
however is completely different: it’s a variable pointing to the current scope. If you execute the following code in the console:
> var myPackage = {}
> myPackage.method = function() {console.log(this)}
> myPackage.method()
此
将是指向 myPackage
对象(方法的范围
)。
这篇关于这个,自我,窗口和窗口之间有什么区别。自己的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!