Four ways to define functions in js function
1. The most basic one is used as a basic function declaration.
Copy code The code is as follows:
function func(){}
or
var func=function(){};
2. Use as a class constructor:
Copy the code as follows:
function class(){}
class.prototype ={};
var item=new class();
3. Use as closure:
Copy the code as follows:
(function(){
//Independent scope
})() ;
4. Can be used as a selector:
Copy code The code 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 judgment
5. Mixed application of the above four situations:
Copy code The code is as follows:
var class=new function(){
var privateArg;//static private variable
function privateMethod=function(){};//static Private method
return function(){/*real constructor*
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn