Home > Article > Web Front-end > js self-executing function
This article shares with you the js self-executing function. Friends in need can refer to it
1 Use self-executing function To wrap the code format
APP = function(){ var a,b; //变量a、b外部不可见 return { add : function(c){ a + b + c; }, format: function(){ //...... } } }()
2 jQuery style anonymous self-executing function
(function(window){ //代码 window.jQuery = window.$ = jQuery;//通过给window添加属性而暴漏到全局 })(window); //通过匿名函数包装代码,所依赖的外部变量传给这个函数,在函数内部可以使用这些依赖,然后在函数的最后把模块自身暴漏给window。如果需要添加扩展,则可以作为jQuery的插件,把它挂载到$上
Related recommendations:
Comparison of several different ways of writing js self-executing functions_javascript skills
The above is the detailed content of js self-executing function. For more information, please follow other related articles on the PHP Chinese website!