问题描述
我看到很多代码,例如:
var myApp ={}; (function() { console.log("Hello"); this.var1 = "mark"; //"this" is global, because it runs immediately on load. Caller is global myApp.sayGoodbye = function() { console.log("Goodbye"); }; })();
导致匿名函数立即执行.但是,与仅包含代码相比,这是什么优点?
var myApp ={}; console.log("Hello"); var1 = "mark"; myApp.sayGoodbye = function() { console.log("Goodbye"); };
显然这与函数的范围有关,但是由于函数是匿名的,并且按窗口调用,因此它的范围(即this)是全局的,否?
推荐答案
通常,您会拥有这样的:
var myApp ={}; (function() { console.log("Hello"); var var1 = "mark"; myApp.sayGoodbye = function() { console.log("Goodbye"); }; })();
主要区别在于var1不会混乱全局名称空间.此通话后,var1仍然与以前相同(通常不确定).
as var1只能从关闭中定义的函数中访问,它被称为"私有".
避免发生冲突的可能原因,这只是更干净的情况,不要在没有用的情况下保留全局变量.
在这里,您没有局部变量,而是一个定义为this.var1的全局变量.这可能是一个错误,或者原因会在代码中的其他地方找到.
其他推荐答案
一个原因:将代码包装在匿名函数中,您可以创建一个模块,该模块将公共API与仅内部用于模块内部使用的私有函数和变量区分开.这避免了污染全局名称空间.
var myApp ={}; (function() { console.log("Hello"); this.var1 = "mark"; function helper() {/*Some code here*/;} myApp.sayGoodbye = function() { helper() console.log("Goodbye"); }; })();
我可以说:
var myApp ={}; console.log("Hello"); var var1 = "mark"; function helper() {/*Some code here*/;} myApp.sayGoodbye = function() { helper() console.log("Goodbye"); };
但随后全局范围包括一个称为helper的函数,对使用您的模块的任何人都不使用,并且可能导致可能与其他模块命名冲突.
我可以将helper作为myApp的一种方法.
var myApp ={}; console.log("Hello"); var var1 = "mark"; myApp.helper = function() {/*Some code here*/;} myApp.sayGoodbye = function() { this.helper() console.log("Goodbye"); };
但是,我可能希望防止用户直接调用helper,在这种情况下,这不会.
问题描述
I see a lot of code like:
var myApp ={}; (function() { console.log("Hello"); this.var1 = "mark"; //"this" is global, because it runs immediately on load. Caller is global myApp.sayGoodbye = function() { console.log("Goodbye"); }; })();
Which causes the anonymous function to execute immediately. But what is the advantage of this, compared to just putting the code inline?
var myApp ={}; console.log("Hello"); var1 = "mark"; myApp.sayGoodbye = function() { console.log("Goodbye"); };
Apparently it's to do with scope of the function, but as the function is anonymous and called by window, it's scope (i.e. this) is global, no?
推荐答案
Usually, you would have this :
var myApp ={}; (function() { console.log("Hello"); var var1 = "mark"; myApp.sayGoodbye = function() { console.log("Goodbye"); }; })();
The main difference is that var1 doesn't clutter the global namespace. After this call, var1 is still the same than before (generally undefined).
As var1 can only be accessed from the function defineds in the closure, it is said "private".
Apart avoiding possible causes of conflicts, it's just cleaner not to keep global variables when useless.
Here, you don't have a local variable but a global one defined as this.var1. It's probably a bug, or the reason would be found elsewhere in the code.
其他推荐答案
One reason: wrapping your code in an anonymous function allows you to create a module which distinguishes a public API from private functions and variables that are only used internally to the module. This avoids polluting the global namespace.
var myApp ={}; (function() { console.log("Hello"); this.var1 = "mark"; function helper() {/*Some code here*/;} myApp.sayGoodbye = function() { helper() console.log("Goodbye"); }; })();
I could say:
var myApp ={}; console.log("Hello"); var var1 = "mark"; function helper() {/*Some code here*/;} myApp.sayGoodbye = function() { helper() console.log("Goodbye"); };
But then the global scope includes a function called helper which is of no use to anyone using your module, and could lead to possible naming conflicts with other modules.
I could alternatively just include helper as a method of myApp.
var myApp ={}; console.log("Hello"); var var1 = "mark"; myApp.helper = function() {/*Some code here*/;} myApp.sayGoodbye = function() { this.helper() console.log("Goodbye"); };
However, I may wish to prevent users from directly calling helper, in which case this won't do.