3. JavaScript Advanced anonymous function

Source: Internet
Author: User

    • Define a function

In JavaScript, you can define a function by "function declaration" and "function expression", such as

1. Define a function by "function declaration"

function T1 () {}

2. Define a function by "function expression"
t2 = function () {}

But there are two ways to define a function, and the effect is different.
T1 is a function declaration, ' Lexical analysis ', ao.t1 = function () {},-------------play a role in the ' lexical analysis ' phase
T2 is an assignment operation, ' run ', ao.t2 = function () {}, the value is the result returned by the expression on the right,------function at ' run ' stage

    • anonymous functions

In JavaScript, the statement in parentheses () is executed as an expression, which says, "function expression" can be used to define a function, so we can define a function within (), such as
(function t3 () {alert (' I am T3 ');})

If the function does not use a name, modify the following
(function () {alert (' I am T3 ');})

the statement contained within the () has a return value, since it is an expression. (function () {alert (' I am T3 ');}) The return value is defined as a function that can be called immediately , as
(function () {alert (' I am T3 ');}) ()


Therefore, a function with no name is defined within the parentheses (), which is called an anonymous function. This technique, the anonymous function, executes immediately, without polluting the global, called an immediate execution function expression .


    • jquery is an anonymous function

jquery's code is encapsulated in an anonymous function, which is the outermost code of jquery:

(function (window,undefined) {}) (window);//Call Now

But why did jquery pass the window and not preach undefined?


A: The window is passed to find the speed and reduce the time of the query variable. For example, the following JS code
function () {
function () {
function () {
function () {
document.getElementById ();//This document will be found along the scope layer until the outermost window is global.
}
}
}
}

jquery is designed to speed up the internal lookup of local variables, and directly passes the window in the form of parameters, so that window is on the internal AO of jquery.

Do not preach undefined is for security, because in the lower version of Ie,ff, undefined unexpectedly can be re-assigned value, such as undefined=3;
Declare local variable undefined (name is undefined), at the same time, do not pass the parameter, the value is naturally undefined


3. JavaScript Advanced anonymous function

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.