为什么在使用它之前在Javascript中声明变量不是必须的? [英] Why isn't it a must to declare a variable in Javascript before using it?

查看:76
本文介绍了为什么在使用它之前在Javascript中声明变量不是必须的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Javascript中,我们不必在使用之前使用 var 关键字声明变量。我们可以马上做一些事情,比如 myCount = 12; myStr =Hello; (其中myCount,myStr是之前未宣布)。任何此类用法,都声明并初始化全局范围内的变量。

In Javascript we don't have to declare a variable with var keyword before using it. We can straight away do things like myCount = 12; or myStr = "Hello"; (where myCount, myStr are not declared before). Any such usage, declares and initializes the variables in the 'global' scope.

提供此功能的原因是什么?它是一个很好的标准吗?

What could be the reasons for providing this feature? And is it a good standard at all?

更新:我的问题不是'使用变量而不声明'和'声明然后使用'以及它如何影响范围。

UPDATE: My question is not what the differences are between 'using a variable without declaring' and 'declaring and then using' and how it affects scope.

我的问题是'为什么允许在javascript中直接使用变量而不声明它'作为大多数编程语言对此有严格的检查。

My question is 'why it is allowed in javascript to use a variable directly without declaring it' as most of the programming languages have a strict check on this.

更新:我看到以下引用的文字是此功能的不良影响。那么,为什么要有这个功能?

UPDATE : I see the following quoted text as a bad effect of this feature. So, why have this feature at all?

假设有一个全局声明的变量 x var x =比较字符串)我已经不知道了,我打算在我的一个函数中创建我自己的全局'x'初始化一个( x = -1 )并且我最终破坏了其他功能。

"Suppose there is a globally declared variable x (var x="comparison string") already which i am unaware of and i with intention of creating my own global 'x' inside one of my functions initialize one(x=-1) and there i end up breaking other functionality.

所以,有什么好的理由吗?拥有这个功能?

So, is there any good reason at all? for having this feature?

推荐答案

Javascript用于浏览器中非常简单的脚本。需要变量声明似乎没必要。

Javascript was intended for very simple scripts in the browser. Requiring variable declarations seemed unnecessary.

当然,这是语言中的一个错误。而javascript的制作者知道。他们想改变它。但他们不能。为什么?

Of course, it's an error in the language. And the makers of javascript know that. They wanted to change it. But they couldn't. Why?

因为微软已经反向设计了JavaScript并创建了他们的重复JScript,带有错误和所有。微软否决了任何更改,甚至是错误修正,因为他们坚持不破坏任何人的脚本。所以甚至如果他们改变了JavaScript,IE中的JScript将保持不变。

Because Microsoft had already reverse engineered JavaScript and created their duplicate JScript, with bugs and all. Microsoft vetoed any changes, even bugfixes, since they were adamant about not breaking anyones scripts. So even if they changed JavaScript, JScript in IE would stay the same.

这不是一个好理由。但它就是我们拥有的那个。

It's not a good reason. But it's the one we have.

来源:我从Douglas Crockford获得了我的javascript历史课:JavaScript编程语言, http://video.yahoo.com/watch/111593/1710507 故事的这一部分是在9到11分钟之间进入视频。

Source: I got my javascript history class from Douglas Crockford: "The JavaScript Programming Language", http://video.yahoo.com/watch/111593/1710507 This part of the story is between 9 and 11 minutes into the video.

这篇关于为什么在使用它之前在Javascript中声明变量不是必须的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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