简体   繁体   English

全局变量和窗口对象的属性有什么区别?

[英]What is the difference between a global variables and attributes of the window object?

I'm a bit confused by JavaScript's window object. 我对JavaScript的窗口对象有点困惑。 For starters, consider the following two declarations: 首先,请考虑以下两个声明:

var imglobal = "I'm Global";

window.imglobal = "I'm Global";

As far as I understand it, this would be exactly the same (is it?) It can be accessed as "imglobal" or "window.imglobal" in both cases. 据我所知,这将完全相同(是吗?)在两种情况下都可以作为“imglobal”或“window.imglobal”访问。 I don't understand why, var declares local variables, the following doesn't work: 我不明白为什么,var声明局部变量,以下不起作用:

function imafunc() {
  var imavar = "I'm a variable";
  window.alert(imafunc.imavar);
}

So why does the following? 那么为什么以下呢?

var imavar = "I'm a variable";
window.alert(window.imavar);

I stumbled across this when using GWT; 我在使用GWT时遇到了这个问题; it appears one always has to refer to the window object ($wnd there) explicitly there, probably because it's not the "real" window object but some kind of sandbox. 它似乎总是必须在那里明确地引用窗口对象($ wnd),可能是因为它不是“真正的”窗口对象而是某种沙盒。

It gets even more confusing with functions, I know three ways to declare them: 它让函数更加混乱,我知道三种方式来声明它们:

var myfunc = function() { window.alert("Hello, World!"); }

window.myfunc = function() { window.alert("Hello, World!"); }

function myfunc() { window.alert("Hello, World!"); }

Is there any technical difference between these three approaches? 这三种方法之间是否存在技术差异?

About your observation: 关于你的观察:

I stumbled across this when using GWT; 我在使用GWT时遇到了这个问题; it appears one always has to refer to the window object ($wnd there) explicitly there, probably because it's not the "real" window object but some kind of sandbox. 它似乎总是必须在那里明确地引用窗口对象($ wnd),可能是因为它不是“真正的”窗口对象而是某种沙盒。

The reason you will always need to prefix your variables and functions with $wnd in GWT JSNI is to ensure that you are accessing the variable from the window scope (host page). 你总是需要在GWT JSNI中使用$ wnd为变量和函数添加前缀的原因是为了确保从窗口范围(主页)访问变量。 This is because JSNI is run inside an iframe, hence any variable without the $wnd qualifier would resolve within the scope of the iframe and not the window scope, which you intended. 这是因为JSNI在iframe中运行,因此没有$ wnd限定符的任何变量都将在iframe的范围内解析,而不是你想要的窗口范围。

There is no differences between global variables and window properties (at least not that much ;)) 全局变量和窗口属性之间没有差异(至少没有那么多;))

The last scope in the scope chain of each function is the window object. 每个函数的作用域链中的最后一个作用域window对象。 Therefore every property of the window object is available in any function without referencing window explicitly. 因此, window对象的每个属性都可以在任何函数中使用,而无需显式引用window

Update: 更新:

I don't understand why, var declares local variables, the following doesn't work: 我不明白为什么,var声明局部变量,以下不起作用:

  function imafunc() { var imavar = "I'm a variable"; window.alert(imafunc.imavar); } 

You cannot access imafunc.imavar because it is not a property of the function. 您无法访问imafunc.imavar因为它不是该函数的属性。 imavar is a property of the activation object , that gets created upon execution of the function and is the "first scope" in the scope chain. imavar激活对象的一个属性,它在执行函数时创建,并且是作用域链中的“第一个作用域”。 You cannot access it directly. 您无法直接访问它。

Btw, to make your function work you have to make imavar a property of the function: 顺便说一下,为了使你的功能工作,你必须使imavar成为函数的一个属性:

function imafunc() {
  this.imavar = "I'm a variable";
  window.alert(imafunc.imavar);
}

The differnt between your three functions is that only the third can called before the declaration in the code, the others are only available till the assigned to variable, cause they are nameless Function Expressions. 你的三个函数之间的不同之处在于,只有第三个函数可以在代码中声明之前调用,其他函数只有在赋值给变量之后才可用,因为它们是无名函数表达式。

myfunc1() and myfunc2() //throws an error cause it isn't declared right now
myfunc3()//will alert(Hello, World!)

var myfunc1 = function() { window.alert("Hello, World!"); }
window.myfunc2 = function() { window.alert("Hello, World!"); }
function myfunc3() { window.alert("Hello, World!"); }

myfunc1()//will alert(Hello, World!)
myfunc2()//will alert(Hello, World!)

http://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/ http://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM