Home Web Front-end JS Tutorial Introduction to javascript anonymous function application examples_javascript skills

Introduction to javascript anonymous function application examples_javascript skills

May 16, 2016 pm 04:56 PM
javascript anonymous function

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 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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Usage and characteristics of C++ anonymous functions Usage and characteristics of C++ anonymous functions Apr 19, 2024 am 09:03 AM

Usage and characteristics of C++ anonymous functions

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

Simple JavaScript Tutorial: How to Get HTTP Status Code

Python Lambda expressions: abbreviated, concise, powerful Python Lambda expressions: abbreviated, concise, powerful Feb 19, 2024 pm 08:10 PM

Python Lambda expressions: abbreviated, concise, powerful

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

How to get HTTP status code in JavaScript the easy way

Can Golang anonymous functions return multiple values? Can Golang anonymous functions return multiple values? Apr 13, 2024 pm 04:09 PM

Can Golang anonymous functions return multiple values?

How are anonymous functions implemented in golang functions? How are anonymous functions implemented in golang functions? Jun 03, 2024 pm 07:09 PM

How are anonymous functions implemented in golang functions?

Python Lambda Expressions: Making Programming Easier Python Lambda Expressions: Making Programming Easier Feb 19, 2024 pm 09:54 PM

Python Lambda Expressions: Making Programming Easier

Python Lambda expressions: Uncovering the power of anonymous functions Python Lambda expressions: Uncovering the power of anonymous functions Feb 24, 2024 am 09:01 AM

Python Lambda expressions: Uncovering the power of anonymous functions

See all articles