JavaScript anonymous function Application Example introduction

Source: Internet
Author: User
Tags anonymous eval

  Anonymous functions, as the name implies that there is no name, there is a good example, you can learn the following

JavaScript anonymous function, which means that a function does not have a name, the following list of test code     code is as follows:/*  * Commonly used functions are such   */  function debug (data) {  Console.log (data); }    But some functions, it happens to write the     code as follows: (function (x, y) {  debug (x + y); }) & nbsp   Above is called anonymous function.     code is as follows: var fun = null;  (function () {  var test = function (x, y) {  debug (x +y); }  Fun =test; }) ();    This time you enter fun in the browser's console, and you will find that it prints out function (x, y) {debug (x +y); Well, obviously, this is a A function, if you enter fun (1, 2); This is the time to print out 3. Let's look at the types below.   Code as follows: var U = {  uid:32812,  gamelist: (function () {  var list = new Array ();  list[7]= ' 360; nbsp List[6]= ' Baidu ';  if (list!= ' null ') {  return list; } } ) (),  serverlist: (function () {& nbsp var list = new Array ();  list[1188]= ' 360 ';  list[1165]= ' Baidu ';  if (list!= ' null ') {  return LIST;&NB Sp } } ) (),  Channellist: (function () {  var list = new Array ();  list[9]= ' hand swim net ';  return list; } ) (),  searchname:function (t,id) {  if (id = False | |/^d+$/.t EST (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; you will find that this 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.