Home  >  Article  >  Web Front-end  >  Introduction to javascript anonymous function application examples_javascript skills

Introduction to javascript anonymous function application examples_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:56:25910browse

JavaScript anonymous function means that the function has no name. The test code is listed below. 🎜>
/*

* Generally common functions are like this */ function debug(data) { console.log(data); }
But some functions are written like this




Copy the code


The code is as follows:


(function(x, y) {

Copy code


The code is as follows:


var fun = null;
(function() { var test = function(x ,y ) { debug(x y); } fun =test; })();
At this time, you enter in the browser console fun, you will find that it prints function(x, y){ debug(x y);}. It is obvious that this is a function. If you enter fun(1, 2);, 3 will be printed at this time. Let’s look at the types below.




Copy code


The code is as follows:


var U = {
uid: 32812, gameList: (function(){ var list = new Array(); list[7]= '360'; list[6]='baidu'; if(list != 'null'){ return list;
}
}
)(),
serverList: (function(){
var list = new Array();
list[1188]='360';
list[1165]='baidu';
if(list != 'null'){
return list;
}
}
)(),
channelList: (function(){
var list = new Array();
list[9]='手机游戏网';
return list;
}
)(),
searchName : function(t,id){
if( id == false || /^d $/.test(id) == false ){
return 'This is a function';
}else if(eval(t).hasOwnProperty(id)){
return eval(t)[id];
}else{
return 'test ';
}
}
};


At this time, you enter U in the console; you will find that this is an array. U['searchName '] is a function, and U['qudaoList'] returns a result.
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