var关键字的目的是什么?我什么时候应该使用它(或省略)?

发布于 2025-02-12 21:43:22 字数 513 浏览 1 评论 0 原文

注释 :从Ecmascript版本3或5的角度提出了这个问题。答案可能会因发行版中引入新功能而过时ecmascript 6。

var 关键字的功能到底是什么

var someNumber = 2;
var someFunction = function() { doSomething; }
var someObject = { }
var someObject.someProperty = 5;

someNumber = 2;
someFunction = function() { doSomething; }
someObject = { }
someObject.someProperty = 5;

JavaScript中

您什么时候使用任何一个,为什么/为什么?

NOTE: This question was asked from the viewpoint of ECMAScript version 3 or 5. The answers might become outdated with the introduction of new features in the release of ECMAScript 6.

What exactly is the function of the var keyword in JavaScript, and what is the difference between

var someNumber = 2;
var someFunction = function() { doSomething; }
var someObject = { }
var someObject.someProperty = 5;

and

someNumber = 2;
someFunction = function() { doSomething; }
someObject = { }
someObject.someProperty = 5;

?

When would you use either one, and why/what does it do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

旧夏天 2025-02-19 21:43:22

如果您处于全球范围,那么没有太大的区别。阅读 kangax's 答案

如果您处于功能中,则 var> var 将创建一个局部变量,“ no var”将查找范围链,直到它找到变量或击中全局范围(在这时它将创建它):

// These are both globals
var foo = 1;
bar = 2;

function()
{
    var foo = 1; // Local
    bar = 2;     // Global

    // Execute an anonymous function
    (function()
    {
        var wibble = 1; // Local
        foo = 2; // Inherits from scope above (creating a closure)
        moo = 3; // Global
    }())
}

如果您不这样做进行分配,然后您需要使用 var

var x; // Declare x

If you're in the global scope then there's not much difference. Read Kangax's answer for explanation

If you're in a function then var will create a local variable, "no var" will look up the scope chain until it finds the variable or hits the global scope (at which point it will create it):

// These are both globals
var foo = 1;
bar = 2;

function()
{
    var foo = 1; // Local
    bar = 2;     // Global

    // Execute an anonymous function
    (function()
    {
        var wibble = 1; // Local
        foo = 2; // Inherits from scope above (creating a closure)
        moo = 3; // Global
    }())
}

If you're not doing an assignment then you need to use var:

var x; // Declare x
美人迟暮 2025-02-19 21:43:22

有差异

var x = 1 在当前范围(又称执行上下文)中声明变量 x 。如果声明出现在函数中 - 声明局部变量;如果它在全球范围中 - 声明了一个全球变量。

另一方面, x = 1 只是属性分配。它首先尝试针对范围链解析 X 。如果它在该范围链中找到任何地方,则执行分配;如果它找不到 x ,则只有才能在全局对象上创建 x 属性(它是范围中的顶级对象链)。

现在,请注意,它不会声明全局变量,而是创建全局属性。

两者之间的区别是微妙的,除非您了解变量声明也会创建属性(仅在变量对象上),并且JavaScript中的每个属性(嗯,Ecmascript)都有某些描述的标志来描述他们的属性 - Readonly,Dontenum和Dontdelete。

由于变量声明使用dontDelete标志创建属性,因此 var x = 1 x = 1 (在全局范围中执行时)之间的差异是前一个 - 变量声明 - 创建DontDelete'able属性,而后者则没有。结果,可以从全局对象中删除通过此隐式分配创建的属性,而前一个对象(通过变量声明创建的对象)无法删除。

但这当然只是理论,而在实践中在实施中,两者之间的差异甚至更大,因为实施中的各种错误(例如来自IE的错误)。

希望这一切都有意义:)


[ES5中的2010/12/16]

(Ecmasixpript 5;最近标准化的该语言的第5版),有一个所谓的“严格模式” - opt-在语言模式下,它稍微改变了未申报的作业的行为。在严格的模式下,分配未确定的标识符是参考eRror 。这样做的理由是捕获偶然的任务,以防止创建不希望的全球属性。一些较新的浏览器已经开始对严格模式进行滚动支持。参见,例如,我的compat Table

There's a difference.

var x = 1 declares variable x in current scope (aka execution context). If the declaration appears in a function - a local variable is declared; if it's in global scope - a global variable is declared.

x = 1, on the other hand, is merely a property assignment. It first tries to resolve x against scope chain. If it finds it anywhere in that scope chain, it performs assignment; if it doesn't find x, only then does it creates x property on a global object (which is a top level object in a scope chain).

Now, notice that it doesn't declare a global variable, it creates a global property.

The difference between the two is subtle and might be confusing unless you understand that variable declarations also create properties (only on a Variable Object) and that every property in Javascript (well, ECMAScript) have certain flags that describe their properties - ReadOnly, DontEnum and DontDelete.

Since variable declaration creates property with the DontDelete flag, the difference between var x = 1 and x = 1 (when executed in global scope) is that the former one - variable declaration - creates the DontDelete'able property, and latter one doesn't. As a consequence, the property created via this implicit assignment can then be deleted from the global object, and the former one - the one created via variable declaration - cannot be deleted.

But this is just theory of course, and in practice there are even more differences between the two, due to various bugs in implementations (such as those from IE).

Hope it all makes sense : )


[Update 2010/12/16]

In ES5 (ECMAScript 5; recently standardized, 5th edition of the language) there's a so-called "strict mode" — an opt-in language mode, which slightly changes the behavior of undeclared assignments. In strict mode, assignment to an undeclared identifier is a ReferenceError. The rationale for this was to catch accidental assignments, preventing creation of undesired global properties. Some of the newer browsers have already started rolling support for strict mode. See, for example, my compat table.

有木有妳兜一样 2025-02-19 21:43:22

说这是“ local global ”之间的区别并不是完全准确的。

最好将其视为“ local 最近的”之间的区别。最近的可以是全球,但情况并非总是如此。

/* global scope */
var local = true;
var global = true;

function outer() {
    /* local scope */
    var local = true;
    var global = false;

    /* nearest scope = outer */
    local = !global;

    function inner() {
        /* nearest scope = outer */
        local = false;
        global = false;

        /* nearest scope = undefined */
        /* defaults to defining a global */
        public = global;
    }
}

Saying it's the difference between "local and global" isn't entirely accurate.

It might be better to think of it as the difference between "local and nearest". The nearest can surely be global, but that won't always be the case.

/* global scope */
var local = true;
var global = true;

function outer() {
    /* local scope */
    var local = true;
    var global = false;

    /* nearest scope = outer */
    local = !global;

    function inner() {
        /* nearest scope = outer */
        local = false;
        global = false;

        /* nearest scope = undefined */
        /* defaults to defining a global */
        public = global;
    }
}
贪恋 2025-02-19 21:43:22

当JavaScript在浏览器中执行时,您的所有代码都被语句所包围,例如:

with (window) {
    //Your code
}

-mdn

因为 var 在当前范围中声明一个变量,声明 var 内部窗口 ,并且根本没有声明它之间没有区别。

当您不直接在窗口内,例如在函数内部或块内部,差异就会出现。

使用 var 允许您隐藏具有相同名称的外部变量。这样,您可以模拟“私有”变量,但这是另一个主题。

经验法则是始终使用 var ,因为否则您会承担引入微妙错误的风险。

编辑:
在收到批评之后,我想强调以下内容:

  • var 在当前范围中声明一个变量
  • 全局范围为 window> window
  • 不使用 var 隐式声明 var 在全局范围(窗口)中
  • 使用 var 在全局范围(窗口)中声明变量省略它。
  • 使用 var 在范围内声明一个与窗口不同的变量与在没有 var> var
  • 始终声明 var 明确是因为这是一个好习惯

When Javascript is executed in a browser, all your code is surrounded by a with statement, like so:

with (window) {
    //Your code
}

More info on with - MDN

Since var declares a variable in the current scope , there is no difference between declaring var inside window and not declaring it at all.

The difference comes when you're not directly inside the window, e.g. inside a function or inside a block.

Using var lets you hide external variables that have the same name. In this way you can simulate a "private" variable, but that's another topic.

A rule of thumb is to always use var, because otherwise you run the risk of introducing subtle bugs.

EDIT:
After the critiques I received, I would like to emphasize the following:

  • var declares a variable in the current scope
  • The global scope is window
  • Not using var implicitly declares var in the global scope (window)
  • Declaring a variable in the global scope (window) using var is the same as omitting it.
  • Declaring a variable in scopes different from window using var is not the same thing as declaring a variable without var
  • Always declare var explicitly because it's good practice
陌生 2025-02-19 21:43:22

始终使用 var 关键字来声明变量。为什么?良好的编码实践本身应该足够的原因,但是省略它意味着它在 global 范围中被声明(这样的变量称为“隐含”全局)。道格拉斯·克罗克福德(Douglas Crockford)建议永远不要使用隐含的全球范围 href =“ https://developer.apple.com/library/archive/documentation/scriptingautomation/conceptual/jcodual/jscodingguide/advanced/advanced.html#//apple_re_reff/doc/doc/doc/doc/uid/uid/uid/uid/uid/tp40006541”指南:

没有 var 创建的任何变量
关键字是在全局范围上创建的
并且不是在
功能返回(因为它没有
走出范围),提出
记忆泄漏的机会。

Always use the var keyword to declare variables. Why? Good coding practice should be enough of a reason in itself, but omitting it means it is declared in the global scope (a variable like this is called an "implied" global). Douglas Crockford recommends never using implied globals, and according to the Apple JavaScript Coding Guidelines:

Any variable created without the var
keyword is created at the global scope
and is not garbage collected when the
function returns (because it doesn’t
go out of scope), presenting the
opportunity for a memory leak.

输什么也不输骨气 2025-02-19 21:43:22

这是一个很好的示例,说明如何从不声明 var

<script>
one();

function one()
{
    for (i = 0;i < 10;i++)
    {
        two();
        alert(i);
    }
}

function two()
{
    i = 1;
}
</script>

i 的每次迭代时,都可以重置本地变量,因为它是它在循环的中未在本地声明,但在全球范围内都会导致无限循环

Here's quite a good example of how you can get caught out from not declaring local variables with var:

<script>
one();

function one()
{
    for (i = 0;i < 10;i++)
    {
        two();
        alert(i);
    }
}

function two()
{
    i = 1;
}
</script>

(i is reset at every iteration of the loop, as it's not declared locally in the for loop but globally) eventually resulting in infinite loop

罪#恶を代价 2025-02-19 21:43:22

我会说最好在大多数情况下使用 var

局部变量总是比全局范围中的变量快。

如果您不使用 var 声明变量,则该变量将在全局范围中。

有关更多信息,您可以在Google中搜索“范围链JavaScript”。

I would say it's better to use var in most situations.

Local variables are always faster than the variables in global scope.

If you do not use var to declare a variable, the variable will be in global scope.

For more information, you can search "scope chain JavaScript" in Google.

能怎样 2025-02-19 21:43:22

不要使用 var

var 是声明变量的前ES6。现在,我们将来是,您应该这样做。

使用 const

const 应用于约95%的情况。它使它变得如此,因此变量引用无法更改,因此数组,对象和DOM节点属性可以更改,并且可能是 const

Let 应用于任何期望重新分配的变量。这包括for循环。如果您曾经编写 varname = 超过初始化,请使用 Let

在大多数其他语言中,两者都有块级别的范围。

Don't use var!

var was the pre-ES6 way to declare a variable. We are now in the future, and you should be coding as such.

Use const and let

const should be used for ~95% of cases. It makes it so the variable reference can't change, thus array, object, and DOM node properties can change and should likely be const.

let should be be used for any variable expecting to be reassigned. This includes within a for loop. If you ever write varName = beyond the initialization, use let.

Both have block level scoping, as expected in most other languages.

遗心遗梦遗幸福 2025-02-19 21:43:22

另一个区别
例如

var a = a || [] ; // works 

a = a || [] ; // a is undefined error.

another difference
e.g

var a = a || [] ; // works 

while

a = a || [] ; // a is undefined error.
凉城凉梦凉人心 2025-02-19 21:43:22

使用 var 始终是一个好主意,以防止变量使整体范围混乱,变量彼此冲突,从而导致不必要的覆盖。

Using var is always a good idea to prevent variables from cluttering the global scope and variables from conflicting with each other, causing unwanted overwriting.

拒绝两难 2025-02-19 21:43:22

没有 var - 全局变量。

强烈建议总是使用 var 语句,因为本地上下文中的init global变量是邪恶的。但是,如果您需要这个肮脏的技巧,则应在页面开头写评论:

/* global: varname1, varname2... */

Without var - global variable.

Strongly recommended to ALWAYS use var statement, because init global variable in local context - is evil. But, if you need this dirty trick, you should write comment at start of page:

/* global: varname1, varname2... */
草莓酥 2025-02-19 21:43:22

这是我为您写的示例代码来理解这个概念:

var foo = 5; 
bar = 2;     
fooba = 3;

// Execute an anonymous function
(function() {    
    bar = 100;             //overwrites global scope bar
    var foo = 4;           //a new foo variable is created in this' function's scope
    var fooba = 900;       //same as above
    document.write(foo);   //prints 4
    document.write(bar);   //prints 100
    document.write(fooba); //prints 900
})();

document.write('<br/>');
document.write('<br/>');
document.write(foo);       //prints 5
document.write(bar);       //prints 100
document.write(fooba);     //prints 3

This is example code I have written for you to understand this concept:

var foo = 5; 
bar = 2;     
fooba = 3;

// Execute an anonymous function
(function() {    
    bar = 100;             //overwrites global scope bar
    var foo = 4;           //a new foo variable is created in this' function's scope
    var fooba = 900;       //same as above
    document.write(foo);   //prints 4
    document.write(bar);   //prints 100
    document.write(fooba); //prints 900
})();

document.write('<br/>');
document.write('<br/>');
document.write(foo);       //prints 5
document.write(bar);       //prints 100
document.write(fooba);     //prints 3
马蹄踏│碎落叶 2025-02-19 21:43:22

@chris s给出了一个很好的例子,展示了 var 和no var 之间的实际差异(和危险)。这是另一个,我发现这特别危险,因为这种差异仅在异步环境中可见,因此在测试过程中可以轻松滑落。

如您所期望的,以下摘要输出 [“ text”]

function var_fun() {
  let array = []
  array.push('text')
  return array
}

console.log(var_fun())

因此,以下片段也是如此(请在 array 之前记下 Let ):

function var_fun() {
  array = []
  array.push('text')
  return array
}

console.log(var_fun())

异步执行数据操作仍然与单个执行人产生相同的结果:

function var_fun() {
  array = [];
  return new Promise(resolve => resolve()).then(() => {
    array.push('text')
    return array
  })
}

var_fun().then(result => {console.log(result)})

但是多种行为的行为不同:

function var_fun() {
  array = [];
  return new Promise(resolve => resolve()).then(() => {
    array.push('text')
    return array
  })
}

[1,2,3].forEach(i => {
  var_fun().then(result => {console.log(result)})
})

但是使用让:

function var_fun() {
  let array = [];
  return new Promise(resolve => resolve()).then(() => {
    array.push('text')
    return array
  })
}

[1,2,3].forEach(i => {
  var_fun().then(result => {console.log(result)})
})

@Chris S gave a nice example showcasing the practical difference (and danger) between var and no var. Here's another one, I find this one particularly dangerous because the difference is only visible in an asynchronous environment so it can easily slip by during testing.

As you'd expect the following snippet outputs ["text"]:

function var_fun() {
  let array = []
  array.push('text')
  return array
}

console.log(var_fun())

So does the following snippet (note the missing let before array):

function var_fun() {
  array = []
  array.push('text')
  return array
}

console.log(var_fun())

Executing the data manipulation asynchronously still produces the same result with a single executor:

function var_fun() {
  array = [];
  return new Promise(resolve => resolve()).then(() => {
    array.push('text')
    return array
  })
}

var_fun().then(result => {console.log(result)})

But behaves differently with multiple ones:

function var_fun() {
  array = [];
  return new Promise(resolve => resolve()).then(() => {
    array.push('text')
    return array
  })
}

[1,2,3].forEach(i => {
  var_fun().then(result => {console.log(result)})
})

Using let however:

function var_fun() {
  let array = [];
  return new Promise(resolve => resolve()).then(() => {
    array.push('text')
    return array
  })
}

[1,2,3].forEach(i => {
  var_fun().then(result => {console.log(result)})
})

故事和酒 2025-02-19 21:43:22

正如某些人试图学习这件事的那样,我就是这样看的。对于初学者来说,上面的示例可能过于复杂。

如果运行此代码:

var local = true;
var global = true;


function test(){
  var local = false;
  var global = false;
  console.log(local)
  console.log(global)
}

test();

console.log(local);
console.log(global);

输出将读取为:false,false,true,true,

因为它将函数中的变量与外部的变量分开,因此术语local变量,这是因为我们在分配中使用了var 。如果您删除函数中的var,因此现在读取这样的读数:

var local = true;
var global = true;


function test(){
  local = false;
  global = false;
  console.log(local)
  console.log(global)
}

test();

console.log(local);
console.log(global);

输出为false,false,false,false,

这是因为而不是在本地范围或函数中创建新变量,而是使用全局变量并重新分配它们错误。

As someeone trying to learn this this is how I see it. The above examples were maybe a bit overly complicated for a beginner.

If you run this code:

var local = true;
var global = true;


function test(){
  var local = false;
  var global = false;
  console.log(local)
  console.log(global)
}

test();

console.log(local);
console.log(global);

The output will read as: false, false, true, true

Because it sees the variables in the function as seperate from those outside of it, hence the term local variable and this was because we used var in the assignment. If you take away the var in the function so it now reads like this:

var local = true;
var global = true;


function test(){
  local = false;
  global = false;
  console.log(local)
  console.log(global)
}

test();

console.log(local);
console.log(global);

The output is false, false, false, false

This is because rather than creating a new variable in the local scope or function it simply uses the global variables and reassigns them to false.

明月松间行 2025-02-19 21:43:22

我看到,在声明有或没有 var 以及功能内部或外部的变量时,人们会感到困惑。这是一个深入的示例,它将引导您浏览以下步骤:

请参阅下面的脚本在此处的jsfiddle

a = 1;// Defined outside the function without var
var b = 1;// Defined outside the function with var
alert("Starting outside of all functions... \n \n a, b defined but c, d not defined yet: \n a:" + a + "\n b:" + b + "\n \n (If I try to show the value of the undefined c or d, console.log would throw 'Uncaught ReferenceError: c is not defined' error and script would stop running!)");

function testVar1(){
    c = 1;// Defined inside the function without var
    var d = 1;// Defined inside the function with var
    alert("Now inside the 1. function: \n a:" + a + "\n b:" + b + "\n c:" + c + "\n d:" + d);

    a = a + 5;
    b = b + 5;
    c = c + 5;
    d = d + 5;

    alert("After added values inside the 1. function: \n a:" + a + "\n b:" + b + "\n c:" + c + "\n d:" + d);
};


testVar1();
alert("Run the 1. function again...");
testVar1();

function testVar2(){
    var d = 1;// Defined inside the function with var
    alert("Now inside the 2. function: \n a:" + a + "\n b:" + b + "\n c:" + c + "\n d:" + d);

    a = a + 5;
    b = b + 5;
    c = c + 5;
    d = d + 5;

    alert("After added values inside the 2. function: \n a:" + a + "\n b:" + b + "\n c:" + c + "\n d:" + d);
};

testVar2();

alert("Now outside of all functions... \n \n Final Values: \n a:" + a + "\n b:" + b + "\n c:" + c + "\n You will not be able to see d here because then the value is requested, console.log would throw error 'Uncaught ReferenceError: d is not defined' and script would stop. \n ");
alert("**************\n Conclusion \n ************** \n \n 1. No matter declared with or without var (like a, b) if they get their value outside the function, they will preserve their value and also any other values that are added inside various functions through the script are preserved.\n 2. If the variable is declared without var inside a function (like c), it will act like the previous rule, it will preserve its value across all functions from now on. Either it got its first value in function testVar1() it still preserves the value and get additional value in function testVar2() \n 3. If the variable is declared with var inside a function only (like d in testVar1 or testVar2) it will will be undefined whenever the function ends. So it will be temporary variable in a function.");
alert("Now check console.log for the error when value d is requested next:");
alert(d);

结论

  1. 无论有或没有var(例如a,b),如果它们在函数之外获得价值,它们都将保留其价值以及通过脚本中各种函数中添加的任何其他值。
  2. 如果变量在一个函数内部没有var(例如C)中声明,则该变量的作用将与以前的规则一样,它将从现在开始将其在所有函数中保留其价值。它要么在函数testVar1()中获得其第一个值
  3. 如果仅在函数内部声明变量(例如testVar1或testVar2中的d),则每当函数结束时,它将不确定。因此,它将是函数中的临时变量。

I see people are confused when declaring variables with or without var and inside or outside the function. Here is a deep example that will walk you through these steps:

See the script below in action here at jsfiddle

a = 1;// Defined outside the function without var
var b = 1;// Defined outside the function with var
alert("Starting outside of all functions... \n \n a, b defined but c, d not defined yet: \n a:" + a + "\n b:" + b + "\n \n (If I try to show the value of the undefined c or d, console.log would throw 'Uncaught ReferenceError: c is not defined' error and script would stop running!)");

function testVar1(){
    c = 1;// Defined inside the function without var
    var d = 1;// Defined inside the function with var
    alert("Now inside the 1. function: \n a:" + a + "\n b:" + b + "\n c:" + c + "\n d:" + d);

    a = a + 5;
    b = b + 5;
    c = c + 5;
    d = d + 5;

    alert("After added values inside the 1. function: \n a:" + a + "\n b:" + b + "\n c:" + c + "\n d:" + d);
};


testVar1();
alert("Run the 1. function again...");
testVar1();

function testVar2(){
    var d = 1;// Defined inside the function with var
    alert("Now inside the 2. function: \n a:" + a + "\n b:" + b + "\n c:" + c + "\n d:" + d);

    a = a + 5;
    b = b + 5;
    c = c + 5;
    d = d + 5;

    alert("After added values inside the 2. function: \n a:" + a + "\n b:" + b + "\n c:" + c + "\n d:" + d);
};

testVar2();

alert("Now outside of all functions... \n \n Final Values: \n a:" + a + "\n b:" + b + "\n c:" + c + "\n You will not be able to see d here because then the value is requested, console.log would throw error 'Uncaught ReferenceError: d is not defined' and script would stop. \n ");
alert("**************\n Conclusion \n ************** \n \n 1. No matter declared with or without var (like a, b) if they get their value outside the function, they will preserve their value and also any other values that are added inside various functions through the script are preserved.\n 2. If the variable is declared without var inside a function (like c), it will act like the previous rule, it will preserve its value across all functions from now on. Either it got its first value in function testVar1() it still preserves the value and get additional value in function testVar2() \n 3. If the variable is declared with var inside a function only (like d in testVar1 or testVar2) it will will be undefined whenever the function ends. So it will be temporary variable in a function.");
alert("Now check console.log for the error when value d is requested next:");
alert(d);

Conclusion

  1. No matter declared with or without var (like a, b) if they get their value outside the function, they will preserve their value and also any other values that are added inside various functions through the script are preserved.
  2. If the variable is declared without var inside a function (like c), it will act like the previous rule, it will preserve its value across all functions from now on. Either it got its first value in function testVar1() it still preserves the value and get additional value in function testVar2()
  3. If the variable is declared with var inside a function only (like d in testVar1 or testVar2) it will will be undefined whenever the function ends. So it will be temporary variable in a function.
黯淡〆 2025-02-19 21:43:22

在代码内部,如果您使用变量而不使用VAR,则会发生的是自动var_name放置在全局范围中:

someFunction() {
    var a = some_value; /*a has local scope and it cannot be accessed when this
    function is not active*/
    b = a; /*here it places "var b" at top of script i.e. gives b global scope or
    uses already defined global variable b */
}

Inside a code you if you use a variable without using var, then what happens is the automatically var var_name is placed in the global scope eg:

someFunction() {
    var a = some_value; /*a has local scope and it cannot be accessed when this
    function is not active*/
    b = a; /*here it places "var b" at top of script i.e. gives b global scope or
    uses already defined global variable b */
}
垂暮老矣 2025-02-19 21:43:22

除了范围问题外,有些人还提及 hoisting ,但没人给出一个例子。这是全球范围的一个:

console.log(noErrorCase);
var noErrorCase = "you will reach that point";

console.log(runTimeError);
runTimeError = "you won't reach that point";

Besides scopes issue, some folks also mention hoisting, but no one gave an example. Here's one for global scope:

console.log(noErrorCase);
var noErrorCase = "you will reach that point";

console.log(runTimeError);
runTimeError = "you won't reach that point";

多彩岁月 2025-02-19 21:43:22

不使用“ var”变量只能定义设置值时才能定义。例如:

my_var;

无法在全球范围中工作。它应该具有类似的价值:

my_var = "value";

另一方面,您可以定义一个有效的类似;

var my_var;

它的值是 undefined (其值不是 null ,它不等于 null 有趣的是。)。

Without using "var" variables can only define when set a value. In example:

my_var;

cannot work in global scope or any other scope. It should be with value like:

my_var = "value";

On the other hand you can define a vaiable like;

var my_var;

Its value is undefined ( Its value is not null and it is not equal to null interestingly.).

挥剑断情 2025-02-19 21:43:22

除非您打算在浏览器中将变量附加到窗口对象上,否则应使用VAR关键字。

变量获取时在不使用var关键字的情况下定义,它看起来是一个简单的“分配”操作。

当该值分配给JavaScript中的变量时,解释器首先尝试在与分配的相同上下文/范围中找到“变量声明”。当解释器执行 dummyvariable = 20 时,它会在函数开始时寻找dummyvariable的声明。
(由于所有变量声明都通过JavaScript解释器移动到上下文的开头,这称为提升)

您也可能想查看中JavaScript

You should use var keyword unless you intend to have the variable attached to window object in browser. Here's a link that explains scoping and difference between glocal scoping and local scoping with and wihtout var keyword.

When variables get defined without the use of var keyword, what it looks like is a simple “assignment” operation.

When the value is assigned to a variable in javascript, the interpreter first tries to find the “variable declaration” in the same context/scope as that of assignment. When the interpreter executes dummyVariable = 20, it looks up for the declaration of dummyVariable at beginning of the function.
(Since all Variable declarations are moved to the beginning of the context by javascript interpreter and this is called hoisting)

You may also want to look at hoisting in javascript

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文