JavaScript anonymous function Application example introduction to _javascript Tips

Source: Internet
Author: User
Tags eval
JavaScript anonymous function, which means that a function does not have a name, let's list the test code
Copy Code code as follows:

/*
* Commonly common functions are like this
*/
function Debug (data) {
Console.log (data);
}

But some functions, it just happens to write this
Copy Code code as follows:

(function (x, y) {
Debug (x + y);
})

It's called the anonymous function.
Copy Code code as follows:

var fun = null;
(function () {
var test = function (x, y) {
Debug (x +y);
}
Fun =test;
})();

This time you enter fun in the console of the browser and you will find that it prints out function (x, y) {debug (x +y);} Well obviously this is a function if you enter fun (1, 2); This is the time to print out 3. Let's look at the types below.
Copy 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]= ' Hand travel net ';
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 ';
}
}
};

This time you enter u in the console and you will find that it is an array. u[' searchname ' is a function, u[' qudaolist '] returns a result.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.