1. What are self-executed anonymous functions?
It refers to a function like this: (function {// code })();
2. Questions
Why (function {// code}) (); can be executed, but function {// code} (); returns an error?
3. Analysis
(1) first, we need to know the differences between the two:
(Function {// code}) is an expression, and function {// code} is a function declaration.
(2). Second, features of js "pre-compilation:
In the "pre-compilation" phase, js will explain the function declaration, but will ignore the table type.
(3 ). when js executes function () {// code} ();, because function () {// code} has been explained in the "pre-compiled" stage, js will skip function () {// code} and attempt to execute ();, so an error will be reported;
When js executes (function {// code}) ();, because (function {// code}) is an expression, js will solve it to get the return value, because the return value is a function, when (); is encountered, it will be executed.
In addition, the method for converting a function to an expression does not have to rely on the grouping operator (). We can also use the void operator ,~ Operator ,! Operator ......
For example:
! Function () {alert ("Alternative anonymous function self-execution ");}();
Callback name function ()() |
(()) |
(function() { alert('hello') })(); (function(v) { alert(v) })('hello'); |
(function() { alert('hello') }()); (function(v) { alert(v) }('hello')); |
Famous function ()() |
(()) |
(function fn() { alert('hello') })(); |
(function fn() { alert('hello') }()); |
Object functions ()() |
Object functions (()) |
({ init:function(){ alert('hello') } }).init(); |
({ init:function(){ alert('hello') } }.init()); |
Object internal () |
Cannot be called like this |
var foo = { fn: function (x) { return x % 2 != 0 ? 'yes' : 'no'; }(1)}; alert(foo.fn); // 'yes' |
var foo = { fn: function (x) { return x % 2 != 0 ? 'yes' : 'no'; }(1)}; alert(foo.fn());
|
Object + "," + Function + () |
The "object" can be as follows. You can try it yourself. |
1, function () { alert('anonymous function is called');}(); |
1. Any number 2. special characters (! ,~ ,-, +) 3. void 4. true and false Of course, these can also be used in combination. |
() Application and Design Mode: Singleton Mode |
Custom object |
// () Apply var Singleton = (function () {function init () {/* here defines the Singleton Code */return {publicMethod: function () {alert ('Hello World') ;}}}return {getInstance: function () {return init ();}};})(); /* call the public method to obtain the instance: */Singleton. getInstance (). publicMethod (); |
Var obj = {}; (function (q) {q. publish = function (str) {alert (str) ;}} (obj); // submit the obj Global Object obj.publish('hello');//hello (Function (a) {a. publish = function (str) {alert (str) ;}} (window); // window object publish ('hello '); |
Famous application example: jQuery |
Design Pattern-iterator Pattern |
(Function () {var jQuery = (function () {// construct the jQuery object var jQuery = function () {// instance init () return new jQuery. fn. init () ;}; jQuery. fn = jQuery. prototype = {constructor: jQuery, init: function () {var match;}, jQuery: "1.7.1"}; // combine the attributes of init () with jQuery. fn jQuery. fn. init. prototype = jQuery. fn; // return jQuery}) (); // defines the jQuery Global Object window. jQuery = window. $ = jQuery;}) (window); // application instance alert ($ (). jQuery); // 1.7.1 |
var fn = (function () { var index = 0, data = [1, 2, 3, 4, 5], length = data.length; return { length: length, rewind: function () { index = 0; }, current: function () { return data[index]; } };} ());alert(fn.current()); |
Select execution object based on parameters |
|
Var fn = (function () {var obj = {i1: function () {return 'a';} (), i2: function () {return 'B ';} ()}; // json format, can be unordered var json = {"1": "i1", "2": "i2"}; // array, ordered Arrangement // var ar = ["i11", "i12"]; return {init: function (I) {return obj [json [I] ;};}) (); alert (fn. init (1 )); |
Return {ini: function (X) {// declare a function named ini X. _ MSG_QS _ = {}; // Add attribute X to the passed object. on = C; // Add method X for the passed object. dm = G; // Add method return X for the passed object }} |
Use closure: (Function ($ ){ // Code goes here }) (JQuery );This is an official jQuery plug-in development standard requirement. What are the advantages of using this writing method? A) avoid global dependency. B) avoid third-party damages. C) compatible with jQuery operators '$' and 'jQuery' We know that this code will be the same as the following code when it is parsed: Var jq = function ($ ){ // Code goes here }; Jq (jQuery ); |
|
|
|