JavaScript anonymous function means that the function has no name. The test code is listed below.
Copy the code code as follows:
/*
*Generally common functions are like this
*/
function debug(data) {
console.log(data);
}
But some functions are written like this
Copy the code code as follows:
(function(x, y) {
debug(x + y);
})
The above is the so-called anonymous function.
Copy the code code as follows:
var fun = null;
(function() {
var test = function(x,y) {
debug(x +y);
}
fun=test;
})();
At this time, if you enter fun in the browser console, 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) ; At this time, 3 is printed. Let’s take a look at the types below.
Copy the code code 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]='Mobile Games Network';
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 it is an array. U['searchName '] is a function, and U['qudaoList'] returns a result.