JavaScript global variables & self-invoking anonymous functions - Stack Overflow

So I've been reading through Javascript - The Good Parts and one thing that Crockford points out i

So I've been reading through Javascript - The Good Parts and one thing that Crockford points out is the use weakness of global variables in Javascript, in such a way that if your product is expanded in some manner, and it relies on a 'global' variable it could be inadvertently set.

That's all good and fine and I understand the pros/cons of protecting variables, in other manners such as closures as well. However, I was doing some thinking, and wrapping code in a function like so:

(function () {
    var x = 'meh';
})();
(function () {
    alert(typeof x); // undefined
})();

gives it variable scope, which thereby prevents cross contamination of variables. I'm not sure if there's a blatant downside to this approach though and wondered if the munity had any input, or if I'm just overthinking things and ignoring the main point.

So I've been reading through Javascript - The Good Parts and one thing that Crockford points out is the use weakness of global variables in Javascript, in such a way that if your product is expanded in some manner, and it relies on a 'global' variable it could be inadvertently set.

That's all good and fine and I understand the pros/cons of protecting variables, in other manners such as closures as well. However, I was doing some thinking, and wrapping code in a function like so:

(function () {
    var x = 'meh';
})();
(function () {
    alert(typeof x); // undefined
})();

gives it variable scope, which thereby prevents cross contamination of variables. I'm not sure if there's a blatant downside to this approach though and wondered if the munity had any input, or if I'm just overthinking things and ignoring the main point.

Share Improve this question edited Oct 28, 2010 at 17:47 Marcel Korpel 21.8k6 gold badges62 silver badges80 bronze badges asked Oct 28, 2010 at 17:21 A Wizard Did ItA Wizard Did It 3,6844 gold badges31 silver badges32 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

That's a perfectly legal way of doing things -- the variables inside of your function (as long as they are prefaced by var) are local to the function. It's called the module pattern, and it's very well accepted.

To create applications with javascript, you must attempt to keep variables in a local scope, and anything inside a namespace. It's a good pratice and prevent a serie of harm codes and unespected behaviors.

read this

it's a article talking about the vantages of doing that.

Making it a global function is not the answer. Why wouldn't you do this? This keeps x out of the global namespace.

(function () {
    var x = 'meh';
    alert(typeof x);  //string
})();
(function (global) {
    global.x = 'meh';
})(window);
(function () {
    alert(typeof x); // string
})();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743745148a4499869.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信