The instant object initialization (ImmediateObjectInitialization) is another method that can prevent global scope & quot; contamination & quot;. It is similar to the immediate execution function of the previous number, that is, the instant object initialization (ImmediateObjectInitialization) mode; in this mode, a party with init () is used...
Object instant Initialization (Immediate Object Initialization)
The other one can prevent global scope "contamination", which is similar to the Immediate execution function of the previous number, namely the Immediate Object Initialization mode;
In this mode, an object with the init () method is used. init () is executed immediately after the object is created;
This init () method will complete all initialization work:
[Javascript]
({
// Here you can define setting values
// A.k. a. configuration constants
Maxwidth: 600,
Maxheight: 400,
// You can also define utility methods
GimmeMax: function (){
Return this. maxwidth + "x" + this. maxheight;
},
// Initialize
Init: function (){
Console. log (this. gimmeMax ());
// More init tasks...
}
}). Init ();
({
// Here you can define setting values
// A.k. a. configuration constants
Maxwidth: 600,
Maxheight: 400,
// You can also define utility methods
GimmeMax: function (){
Return this. maxwidth + "x" + this. maxheight;
},
// Initialize
Init: function (){
Console. log (this. gimmeMax ());
// More init tasks...
}
}). Init (); from the syntax point of view, using this mode is similar to using the object literal to declare a common object;
You can also wrap the literal in a pair of parentheses, which tells the JavaScript engine to process the braces as the object literal instead of the code block;
Call the init () method immediately after ending the parentheses.
You can also wrap the init () method call into parentheses, instead of containing only objects;
In other words, both of the following conditions can work properly:
[Javascript]
({...}). Init ();
({...}. Init ());
({...}). Init ();
({...}. Init (); the benefits of this mode are the same as the immediate execution of the function mode: when you perform a one-time initialization, the global scope is protected (no global variables are added );
In syntax, the real-time object initialization may seem more complex than the anonymous function of only wrapped code;
However, if your initialization work is more complex (in fact, they often do this), you will add code to the entire initialization process;
For example, private helper functions are clearly distinguished because they are attributes of this temporary object;
However, in the immediate execution of functions, they are probably scattered functions. Www.2cto.com
One disadvantage of this mode is that most JavaScript compression tools do not compress the code efficiency in the function when compressing this mode;
Private attributes and methods are not renamed with shorter names, because they are insecure from the perspective of compression tools.
This mode is suitable for one-time work, and the object will not be accessed after the init () call completes the method;
How do you want to hold a reference to this object after the init () method is executed? You can easily implement this function by adding return this at the end of the init () method;