Learn Yui.ext the next day _yui. Ext Related

Source: Internet
Author: User
Tags anonymous closure
Encountered a number of JS grammar problems, do not understand thoroughly, bad repair basic skills!



Anonymous functions an anonymous function

No name is called "anonymous function", like this

function (x,y) {return x+y}

No name, of course, can not be called directly, can not be invoked, up to only the assignment or closure of the package (what is the closure below), such as:

var sum =function (x,y) {return x+y};

Alert (sum (1,2));

At this point, the function sum (x,y) {return x+y}, which is equivalent to the traditional notation, is the same. This kind of writing makes people feel more oop, because the sum variable contains function ... This function body;

You can also call the function in a closed way:

(Functioin (x,y) {return x+y}) (1,2)//return value 3

The code is concise. Note the use of parentheses, in the form of (exp) (). This usage can be called a closure closure.
The back brackets are the arguments, and the parameters are put into the FN and immediately computed to produce a value of 3. This is actually an expression of an operation. I did not think the FN function body can also be put in to participate in the operation ^_^ (Using function as an expression)! (Basic: Expression expression, which means that after calculation, always returns a value, regardless of how long the expression is)

FN can also be passed as a parameter (passing function as argument to the other functions)

var main_fn = function (fn,x,y) {return fn (x,y)}

var sum = function (x,y) {
return x+y;
}

Alert (MAIN_FN (sum,1,2))//Result:3

Summing up (by an IBM Engineer's article, refer to IBM website, the best mind to remember)

Functions need not have names all the time.

Functions can be assigned to variables as other values.

A function expression can is written and enclosed in parenetheses for application later.

Functions can is passed as arguments to Oher funcitons.

To talk about closure, the function of closure is to form a domain of definition, to give an idiotic example of 1+ (2+3), bracket part precedence, or in other words, the parentheses inside a range, this range I don't care what you do is what you do, not the parentheses outside (like nonsense, I think, This is how it is written @#@), and so is the procedural understanding. JS has functions that define domain function scope, so when you use this to point to an object with a problem, consider using a closure. Concrete examples in: http://www.svendtofte.com/code/practical_functional_js/

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.