Function expression
( Function (window, U) { // pass undefined Alert ( "AS" ); }) (window); // reason for calling a global variable transfer function expressions anonymous function immediately executed without the need to pass a global variable during execution skip to seek outside time-consuming
Function runs therein, the key three objects
AO scope chain formed
argument does not form chains
this form prototype chain
arguments
(function(window,u){ var sum=0; for(var i=2;i<arguments.length;i++){ sum+=arguments[i];//不定参数 } console.log(sum); })(window,undefined,1,2,1,1,2,12,1,1,1,1,1);
arguments.callee as a function of its own
console.log((function(window,u){ if(u>1){ return u+(arguments.callee)(window,u-1); }else{ return 1; } })(window,100));
Note distinguish between the two differences
// "use strict" // Use ES6 grammatically var Age =. 11 ; function Test () { this .age 12 is = ; } var S = new new Test (); // At this time, compared to null if this is null window // with this new function should be operating within or will pollute the global variable console.log (age);
result
// "use strict" // Use ES6 grammatically var Age =. 11 ; function Test () { this .age 12 is = ; } Test (); // this case this is null when compared to null window // Function within with this operation should be new or will pollute the global variable console.log (age);
result
You can see directly when calling the default is not applicable ES6 syntax requires null at this time this is the window contamination global variable variable If this new object is created from the top will not pollute the global
var name="webcyh"; function test(){ console.log(this.name+"hehe"); } var dog={name:"dog"}; var cat={name:"cat"}; dog.intro=test; dog.intro(); cat.intro=dog.intro; cat.intro(); (dog.intro=test)();
Construction method
function Cat (name) { the this .name = name; } var C = new new Cat (); / * generate cavitation object {} new new the this method is directed} { {} .name = name; * /