One statement: javascript anonymous functions)

Source: Internet
Author: User

The concept of anonymous functions may not be unfamiliar to everyone, but I believe the following content will open up some new ideas.

We know that the function can be defined in two ways:

1 function fn1 () {alert ('fn1 works ');}

2 var FN2 = function () {alert ('fn2 works ');}

What are the differences between the two methods? Think about it...

1. Define a function in the first way. The function declaration process completes preprocessing before the execution of the entire program. Therefore, as long as the function is in the same scope, you can access the function, it can be called even before the definition.

2. In chapter 2, the method is anonymous functions. In this way, a function can only be declared after the code is defined according to the program flow. Therefore, it can only be called after the definition.

For example

VaR fn1;

Fn1 (); // Error

Fn1 = function (){

FN2 (); // FN2 works

Alert ('fn1 works ');

Return false;

Function FN2 (){

Alert ('fn2 works ');

}

}

Fn1 (); // FN2 works + fn1 works

Although FN2 is after return, the program process is not executed, but it can still be used. On the contrary, FN2 can be normally executed only after it is defined.

Now, let's take a look at the use of anonymous functions in recursion.

Recursion is the internal call of the function. For example

Function fn1 (n) {return n> 2? Fn1 (n-1) + fn1 (n-2): N ;}

When we want to assign this function to other object attributes, we need to use anonymous functions, such

Obj1 = {

FN: function (n ){

Return n> 2? Obj1.fn (n-1) + obj1.fn (n-2): n ;}} obj2 = {fn: obj. FN ;}
You can call obj2.fn () Normally at this time, but there is a hidden risk. We must ensure that the FN in obj1 cannot be overwritten. See the example below.
Obj1 = {} obj2.fn ();
The obj1 is cleared and an error occurs during execution. There are many solutions to this problem. You should first think of one... The easiest thing to think of is to use this. Change the FN definition in obj1 as follows:

Obj1 = {

FN: function (n ){

Return n> 2? This. FN (n-1) + this. FN (n-2): n ;}}
In this way, even if the FN in obj1 is rewritten, The obj2.fn () is not affected. But is this confusing? This is definitely a hard-to-understand concept in JavaScript and is the best to understand, but in many cases it is not necessary to use this. In this example, we can select a name for an anonymous function.

Obj1 = {

FN: function fnname (n ){

Return n> 2? Fnname (n-1) + fnname (n-2): n ;}}
We found that the result is the same as that of this, but this avoids the audio and video of this. Note that this function name can only be accessed inside the function and cannot be accessed outside the function (Please test it in private ), in this way, problems such as global variables can be avoided. There is also a solution, which is the most elegant, so I want to give it to you as a gift. We know there is an arguments object, which has many interesting attributes and methods, one of them is callee, which calls the function itself! Continue to modify the above example

Obj1 = {

FN: function (n ){

Return n> 2? Arguments. callee (n-1) + arguments. callee (n-2): n ;}}
This is the most elegant method I have found. You can discuss other better solutions. Anonymous functions are discussed here first. Reprinted, please note: large front-end»
One story: javascript anonymous Functions

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.