4 ways to define a function by js function
1. The most basic use is as a function declaration of the book.
The code copy is as follows:
The code copy is as follows:
function func(){}
or
var func=function(){};
2. Use as a class constructor:
The code copy is as follows:
The code copy is as follows:
function class(){}
class.prototype={};
var item=new class();
3. Use as a closure:
The code copy is as follows:
The code copy is as follows:
(function(){
//Independent scope
})();
4. Can be used as a selector:
The code copy is as follows:
The code copy is as follows:
var addEvent=new function(){
if(!-[1,]) return function(elem,type,func){attachEvent(elem,'on'+type,func);};
else return function(elem,type,func){addEventListener(elem,type,func,false);}
};//avoid repeated judgments
5. Mixed applications of the above four situations:
The code copy is as follows:
The code copy is as follows:
var class=new function(){
var privateArg;//static private variable
function privateMethod=function(){};// Static private method
return function(){/*Real constructor*