为什么在javascript中使用var关键字 [英] why var keyword is used in javascript

查看:53
本文介绍了为什么在javascript中使用var关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在javascript中使用var关键字而不是使用字符串或数字

解决方案

Var 表示变量。为什么在JavaScript中使用它的部分解释如下: http://www.w3schools.com/js/js_variables。 asp [ ^ ] (不推荐谢尔盖在回答的评论中提到)



欲了解更多信息,请参阅:

http://www.tutorialspoint.com/javascript/javascript_variables.htm [ ^ ]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types [ ^ ]





一如既往,维基百科急于抢救溺水;)

< a href =http://en.wikipedia.org/wiki/JavaScript_syntax#Variables> http://en.wikipedia.org/wiki/JavaScript_syntax#Variables [ ^ ]



引用:



标准JavaScript中的变量没有附加类型,任何值都可以存储在任何变量中。使用var语句声明变量,可以一次声明多个变量。





因此,一个关键字用于声明许多数据类型;)


请考虑以下事项:

  var  x =  外部范围; 

function (){
alert(x);
var x = 内部范围;
})()



您认为该警报会输出外部范围吗?没有!只有在取消注释内部 var x 时才会发生这种情况。但是在语句之后编写的代码如何影响呢?是的,它可以。 JavaScript是一件非常重要的事情;它被认为是世界上被误解最多的语言。第二个 var 声明告诉解释器使用内部作用域,这使前一行上的 x 未定义。因此,JavaScript不是只能逐行解释和执行所有内容的解释器。



var 关键字是用于解决范围。此外,在JavaScript 严格模式中,我们认为 var 是必须的。在v.1.7中引入的新 let 关键字的行为有所不同;如果您在我显示的函数中使用 let 而不是 var ,则会产生错误。与 var 不同,对词法范围敏感。



所有这些功能都是努力为JavaScript编程活动提供更好的支持,使其更可靠。



https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var [ ^ ],

https://developer.mozilla。 org / zh-CN / docs / Web / JavaScript / Reference / Strict_mode [ ^ ],

https://developer.moz illa.org/en-US/docs/Web/JavaScript/Reference/Statements/let [ ^ ]。



-SA

Why var keyword is used in javascript rather than using string or number

解决方案

Var means variable. Why it is used in JavaScript is partially explained here: http://www.w3schools.com/js/js_variables.asp[^] (not recommended as Sergey mentioned in the comment to the answer)

For further information, please see:
http://www.tutorialspoint.com/javascript/javascript_variables.htm[^]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types[^]

[EDIT]
As always, Wikipedia rush to rescue of drowning ;)
http://en.wikipedia.org/wiki/JavaScript_syntax#Variables[^]

Quote:


Variables in standard JavaScript have no type attached, and any value can be stored in any variable. Variables are declared with a var statement, multiple variables can be declared at once.



So, one keyword is used to declare many data types ;)


Consider the following:

var x = "outer scope";

(function() {
     alert(x);
     var x = "inner scope";
})()


Do you think that alert will output "outer scope"? No! It will happen only if you uncomment inner var x. But how the code written after the statement can affect it? Yes, it can. JavaScript is a very non-trivial thing; it is said to be the most misunderstood language in the world. The second var declaration tells the interpreter to use inner scope, which makes x on previous line undefined. So, JavaScript is not the interpreter which merely interprets and executes everything line by line.

The var keyword is used to resolve scope. Moreover, in JavaScript strict mode it is considered, var is made a must. And new let keyword introduced in in v.1.7 acts differently; if you used let instead of var in the function I show, it would generate an error. Unlike var, let is sensitive to the lexical scope.

All these features are the part of effort to introduce better support of JavaScript programming activity, to make it more reliable.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var[^],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode[^],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let[^].

—SA


这篇关于为什么在javascript中使用var关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆