Home > Web Front-end > JS Tutorial > Several good ways to define functions in js function_javascript skills

Several good ways to define functions in js function_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 16:57:56
Original
1018 people have browsed it

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:

Copy code The code is as follows:

function func(){}
or
var func=function(){};

2. Use as a class constructor:

Copy the code as follows:
Copy code The code is as follows:

function class(){}
class.prototype ={};
var item=new class();

3. Use as closure:

Copy the code as follows:
Copy code The code is as follows:

(function(){
//Independent scope
})() ;

4. Can be used as a selector:

Copy code The code is as follows:
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:
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*
Related labels:
Statement of this Website
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
Latest Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template