Home > Web Front-end > JS Tutorial > JavaScript anonymous function usage analysis_javascript skills

JavaScript anonymous function usage analysis_javascript skills

WBOY
Release: 2016-05-16 16:14:15
Original
1180 people have browsed it

The examples in this article describe the usage of JavaScript anonymous functions. Share it with everyone for your reference. The details are as follows:

1. Define a function

In JavaScript, a function can be defined through "function declaration" and "function expression", such as

1. Define a function through "function declaration"

function t1(){}
Copy after login

2. Define a function through "function expression"

t2 = function(){}
Copy after login

But the effects of defining functions in two ways are different
t1 is a function declaration. During ‘lexical analysis’, AO.t1 = function(){},-------------it will play a role in the ‘lexical analysis’ stage
t2 is an assignment operation. During 'run', AO.t2 = function(){}, the value is the result returned by the expression on the right, ------it only comes into play in the 'run' stage

2. Anonymous function

In JavaScript, statements in parentheses () are executed as expressions. As mentioned above, you can use "function expressions" to define a function. Then, we can define a function within (), such as

(function t3(){alert(' i am t3');})
Copy after login

If the function does not use a name, modify it as follows

(function(){alert(' i am t3');})
Copy after login

Since the statement contained in () is an expression, it has a return value. The return value of (function(){alert(' i am t3');}) is the defined function, which can be called immediately, such as

(function(){alert(' i am t3');})()
Copy after login

Therefore, define a function without a name within parentheses (), which is called an anonymous function. In this way, anonymous functions are executed immediately without polluting the global situation, which is called immediate execution of function expressions.

3. jquery is an anonymous function

The code of jquery is encapsulated in an anonymous function. This is the outermost code of jquery:

(function(window,undefined){})(window);//立即调用
Copy after login

But why does jquery pass window but not undefined?

Answer: The purpose of passing window is to speed up search and reduce the time of querying variables. For example, the following js code

function(){
 function(){
   function(){
  function(){
   document.getElementById();
//这个document将会沿作用域层层上找,直到最外层window全局。
  }
   }
 }
}
Copy after login

In order to speed up the internal search of local variables, jquery directly passes the window in as a parameter, so that the window is on the internal AO of jquery.

The reason for not passing undefined is for safety, because in lower versions of IE and FF, undefined can be reassigned, such as undefined=3;

Declare the local variable undefined (the name is undefined), and at the same time, without passing parameters, the value will naturally be undefined

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template