Javascript 窗口.变量名

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

window.variableName

javascript

提问by Jon

I am going through some code and at the beginning of the script we have var emailID = email. Later on, the code refers to emailID by going window.emailID. I am wondering what are the rules that allow you to refer to a variable by going window.variableName?

我正在浏览一些代码,在脚本的开头我们有var emailID = email. 稍后,代码通过 go 来引用 emailID window.emailID。我想知道允许您通过 window.variableName 引用变量的规则是什么?

I cannot post my script online as it is directly related to my work and would violate my contract.

我不能在网上发布我的脚本,因为它与我的工作直接相关并且会违反我的合同。

回答by marteljn

window.variableNamemeans that the variable is being declared at the global scope. This means any JS code will have access to this variable. Using window.is not necessary but is frequently used as a convention to denote that a variable is global.

window.variableName意味着该变量是在全局范围内声明的。这意味着任何 JS 代码都可以访问这个变量。使用window.不是必需的,但经常用作表示变量是全局变量的约定。

Globals are generally to be avoided. You should define variables within the scope of functions.

通常要避免使用全局变量。您应该在函数范围内定义变量。

回答by Sergey

Global variables in JavaScript are attached to the "global object", which in a browser environment is aliased to windowobject - this is why you can refer to a global variable either as variableNameor window.variableName.

JavaScript 中的全局变量附加到“全局对象”,在浏览器环境中它是window对象的别名——这就是为什么你可以用variableName或来引用全局变量window.variableName

It's also worth mentioning that using global variables in JavaScript is not considered good coding practice.

还值得一提的是,在 JavaScript 中使用全局变量不被认为是良好的编码习惯。

Here's a good and very detailed explanation.

这是一个很好且非常详细的解释

回答by Matt Coughlin

window.myVaror window["myVar"]is an explicit way to refer to a global variable.

window.myVar或者window["myVar"]是引用全局变量的显式方法。

A variable is a global variable if it's declared outside of a function (with or without "var"), or if it's declared inside a function without using "var", or if it's declared as window.myVaror window["myVar"].

一个变量是全局变量,如果它是在函数外部声明的(有或没有“var”),或者如果它在函数内部声明而不使用“var”,或者如果它被声明为window.myVaror window["myVar"]

A variable is declared by either assigning a value to it, or by using the keyword var.

变量是通过为其赋值或使用关键字来声明的var

One case where it's useful to refer to a global variable as window.myVaris if you're inside a function that has a local variable called myvar. In that case, myVarrefers to the local variable, while window.myVarrefers to the global variable.

在一种情况下,引用全局变量很有用,window.myVar就像您在具有名为myvar. 在那种情况下,myVar指的是局部变量,而window.myVar指的是全局变量。

回答by Nanhe Kumar

Global Variables in JavaScript

JavaScript 中的全局变量

var no =10;
function getNo()
   alert(no); // 10
}
getNo();

When a global variable is set, it's added to the window object!

当设置了全局变量时,它会被添加到 window 对象中!

var no =10;
function getNo()
   alert(window.no); // 10
}
getNo();

We can direct set window variable.

我们可以直接设置窗口变量。

function setNo(){
  window.no=100;
}
function getNo()
   alert(window.no); // 100
}
setNo();
getNo();

回答by Eugen

For pure theoretical explanations (as I encountered this "issue" myself) and easy to digest information you can look at the problem as this:

对于纯理论解释(因为我自己遇到了这个“问题”)和易于消化的信息,您可以将问题视为这样:

var myName = "Bob" equals to - globalObject(Window) = { myName: "Bob" }

var myName = "Bob" 等于 - globalObject(Window) = { myName: "Bob" }

so when you declare a variable, that variable name is passed to the window object as a property and it's value as a property value. That's why you can call the variable as an object method of the window object, basically.

因此,当您声明一个变量时,该变量名称将作为属性传递给 window 对象,并将其值作为属性值传递。这就是为什么基本上可以将变量作为 window 对象的对象方法来调用。

回答by Praveen Kishor

It is used to define global variable in JavaScript.

它用于在 JavaScript 中定义全局变量。

globalVar = "Hello World";
function function1(){
    alert(window.globalVar);
} 
function1();

This will print "Hello World" in the popup.

这将在弹出窗口中打印“Hello World”。

    function function1(){ 
        globalVar = "Hello World";
        alert(window.globalVar);
    }function function2(){
        alert(window.globalVar);
    } 
    function1(); 
    function2();

This will create two popups with value "Hello World", one from function1() and another from function2().

这将创建两个值为“Hello World”的弹出窗口,一个来自 function1(),另一个来自 function2()。

So, by using window we can call a variable from any scope in javascript.

因此,通过使用 window 我们可以从 javascript 中的任何范围调用变量。