Using javascript anonymous functions to analyze several simple and interesting code _ javascript skills

Source: Internet
Author: User
I think of my experience learning javascript a long time ago, and I have encountered several problems caused by anonymous functions (one of which is caused by closures). I will sort out several simple code sections to discuss, let us all make progress together. 1. Simple encapsulation call

The Code is as follows:


Var userName = function () {return "jeff wong "}();
Alert (userName );



The above code is indeed simple. We can gradually break it down into the following code:

The Code is as follows:


Var anonymousFunc = function () {return "jeff wong"}; // anonymous function
Var name = anonymousFunc (); // execute this function to return the person's name
Alert (name );


2. new Function Form (capital Function)

The Code is as follows:


Var a = new Object ();
Var B = new Function ();
// Alert (typeof (a); // object
// Alert (typeof (B); // function
Alert (a); // [object Object]
Alert (B); // anonymous Function
// Alert (a = B); // false
// Alert (a = B); // false



As you can see, when we create a new Object, variable a pops up [object Object], and new Function (Note: it is an upper-case Function), and B pops up, an anonymous function is generated. Since B is an anonymous function, the function can certainly be executed. We can continue to try the following code to verify our guess:

The Code is as follows:


Alert (B (); // undefined
Alert (a (); // script error message "function missing"



3. A new function has a lower-case function)
(1) Simple empty Functions

The Code is as follows:


Var func = new function (){};
Alert (typeof (func); // object
Alert (func); // [object Object]
// Alert (func (); // Script Error func is not a function



In fact, the above Code is equivalent to the following statement:

The Code is as follows:


Function anonymousClass () {}// Anonymous class
Var instance = new anonymousClass ();
Alert (typeof (instance); // object
Alert (instance); // [object Object]

[Code]

(2) Functions with return values are not hard to understand.
[Code]
Var func = new function () {return "jeff wong "};
Alert (typeof (func ));
Alert (func );
// Alert (func (); // The function is missing due to a script error.



In fact, the above Code is equivalent to the following statement:

The Code is as follows:


Function anonymousClass () {return "jeff wong";} // Anonymous class
Var instance = new anonymousClass ();
Alert (typeof (instance); // object
Alert (instance); // [object Object]



(3) If the function still has a return value, the statement is slightly different.

Note the following code to distinguish it from (2), because next we will focus on the different forms of writing:

The Code is as follows:


Var func = new function () {return new String ("jeff wong ");};
Alert (typeof (func); // expected object
Alert (func); // here ?!
// Alert (func (); // The function is missing due to a script error.



The equivalent form of the above Code is still simple:

The Code is as follows:


Function anonymousClass () {return new String ("jeff wong ");}
Var instance = new anonymousClass ();
Alert (typeof (instance ));
Alert (instance );


Have you seen the result after running? That's right. In the third way, when func or instance is popped up, we all unexpectedly get a string "jeff wong ". Carefully compare the code in (2) and (3). Except for the slightly different code in return, the two codes are almost identical. Therefore, we can infer that there is no doubt that, it is the form of new String, which makes our function produce unexpected results. Why?

Originally, in javascript, as long as the constructor after the new expression returns (return) an original type (when there is no return, it is actually the original return type undefined, such as (1 )), for example, if the expression (2) is used, the newly created anonymous Object is returned. If the constructor after the new expression returns a reference Object, such as an Object or function) and Array (Array), the returned reference object will overwrite the new anonymous object. Now let's analyze the Writing Method in (3). Because new String constructs a String reference object, it overwrites the anonymous object created by new, the reference value of the new String is "jeff wong", so the pop-up result must be the value allocated by the current new String.

Finally, let's take a look at the results returned by the following code:

The Code is as follows:


Var func = new function () {var str = new String ("jeff wong"); return str ;}; // rewrite
// Alert (typeof (func); // expected object
Alert (func); // guess what the result is?


Author: Jeff Wong
Related Article

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.