防止在Javascript中自动创建全局变量 [英] Preventing auto-creation of global variables in Javascript

查看:75
本文介绍了防止在Javascript中自动创建全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了一些时间来调试一个问题,该问题归结为忘记在新的变量标识符前面使用 var 关键字,因此Javascript自动创建了该变量全球范围。有没有办法防止这种情况,或改变默认行为,而不使用像JSLint这样的验证器?

I just spent some time debugging a problem that boiled down to forgetting to use the var keyword in front of a new variable identifier, so Javascript was automatically creating that variable in the global scope. Is there any way to prevent this, or change the default behavior, without using a validator like JSLint?

在编写和执行Javascript代码之间运行验证器似乎很差编译的借口,这是我通常依赖的一步来捕捉这类事情。

Running a validator in between writing and executing Javascript code seems like a poor excuse for compiling, which is the step I'd usually rely on to catch this sort of thing.

我猜答案是不,所以我我正在研究JSLint Eclipse插件,因为我发布了这个。

I'm guessing the answer is a "no," so I'm looking into a JSLint Eclipse plugin as I post this.

推荐答案

ES5严格模式阻止自动创建全局变量,但它可能还需要一年才有任何识别严格模式的运输浏览器,所以在此之前JSLint可能是你最好的选择: - /

ES5 strict mode prevents automatic creation of global variables, but it's probably going to be a year before there are any shipping browsers that recognise strict mode, so JSLint is probably your best bet until then :-/

这篇关于防止在Javascript中自动创建全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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