Learning YUI. Ext the next day

Source: Internet
Author: User

I encountered some JS Syntax problems, but I didn't fully understand them. It's a basic complement!

Anonymous function An anonymous function

An anonymous function is called without a name.

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

Without a name, you cannot call it directly or call it. You can assign a value or handle a closure at most (what is a closure?), for example:

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

Alert (sum (1, 2 ));

At this time, it is equivalent to the traditional function sum (x, y) {return x + y }. this method makes people feel more OOP, because the sum variable contains the function... this function body;

You can also use a closure to call this function:

(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 method can be called closure.
Parameters are enclosed in brackets. Put these parameters in fn and calculate them immediately to get a value of 3. This is actually an expression operation. I didn't expect the fn function body to be included in the computation ^_^ (Using function as an expression )! (Basic function: Expression, which means that after calculation, a value is always returned regardless of the length of the Expression)

Fn can also be passed in the form of parameters (passing function as argument to 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

To sum up (by an IBM Engineer's article, refer to IBM website, it is best to remember)

Functions need not have names all the time.

Functions can be assigned to variables like other values.

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

Functions can be passed as arguments to oher funcitons.

Let's talk about the closure. The function of the closure is to form a definition field. For example, 1 + (2 + 3) is an idiotic example, and parentheses are partially prioritized. Or let us change the argument, the brackets are classified as a range. I ignore what you do and it is not related to the outside world (it seems like nonsense, -- I think so, it is written in this way @ # @), so is the understanding in the program. Js has a function defined Domain function scope. Therefore, when using this to point to an object, consider using a closure. Specific examples in: http://www.svendtofte.com/code/practical_functional_js/

Related Keywords:

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.