开发者

Prevent declaring variables without var

How can I prevent variables from being declared without using the var keyword? Can I set N开发者_运维知识库etBeans to warn me about this?

For example, I want a warning or something on this:

var testing = 5;

// ...

testin = 2; // Woops, typo!


I can think of at least a couple of possibilities for you. One is JSLint, as mentioned above. Another is to use the new Javascript feature called "strict mode" (described here among other places)

I have heard that the nightly builds of Firefox now support strict mode, as do the pre-release versions of Chrome. Soon all new browser versions will support it.


I just came across this question and thought it worth noting that ECMAScript 5 now has an option called "use strict". Just put "use strict"; (including the quotes) on a line by itself either at the top of your script (to use it everywhere) or within a function to just use it in one spot. See http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/ for more details. Extremely handy!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜