Using JavaScript's anonymous function to analyze a few pieces of simple and interesting code _javascript tips

Source: Internet
Author: User
Tags anonymous object object
1, simple form of encapsulation call
Copy Code code as follows:

var userName = function () {return "Jeff Wong"} ();
alert (userName);


The above code is really simple, we can gradually break down into the following wording:
Copy Code code as follows:

var anonymousfunc = function () {return "Jeff Wong"}; anonymous functions
var name = Anonymousfunc (); Perform this function to return a person's name
alert (name);


2, the form of new function (capital function)
Copy Code code 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 functions
Alert (A = = B); False
Alert (A = = = B); False



As you can see, we're new object, variable a pops up [object], and the new function (note, the uppercase function), B generates an anonymous function when it pops up. Since b is an anonymous function, of course, the function can be executed, we can continue to try the following code to verify their guess:
Copy Code code as follows:

Alert (b ()); Undefined
Alert (A ()); Script error Prompts "missing function"



3, the new function is also a great deal of the universe (lowercase function)
(1), simple null function
Copy Code code 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 wording:
Copy Code code as follows:

function Anonymousclass () {}//anonymous class
var instance = new Anonymousclass ();
Alert (typeof (instance));//object
alert (instance); [Object Object]

[Code]

(2), the function with a return value, it is not difficult to understand
[Code]
var func = new function () {return "Jeff Wong"};
Alert (typeof (Func));
Alert (func);
Alert (func ()); Script Error missing function



In fact, the above code is equivalent to the following wording:
Copy Code code as follows:

function Anonymousclass () {return "Jeff Wong";}//Anonymous class
var instance = new Anonymousclass ();
Alert (typeof (instance));//object
alert (instance); [Object Object]


(3), or function with a return value, the wording is slightly different

The following code takes note of the distinction from (2), because the next thing to focus on is that little bit of different writing:
Copy Code code as follows:

var func = new function () {return new String ("Jeff Wong");
Alert (typeof (Func)); Object expected
Alert (func); Over here?!
Alert (func ()); Script Error missing function



The equivalent form of the above code is still simple:
Copy Code code as follows:

function Anonymousclass () {return new String ("Jeff Wong");}
var instance = new Anonymousclass ();
Alert (typeof (instance));
alert (instance);


Already running see the results? That's right, the third way, when we popped up Func or instance, we were surprised to get a string of "Jeff Wong." Careful comparison of the code in (2) and (3), except for a slight difference in the way the return was written, the two codes are almost identical, so we infer that it is no doubt that the form of new string gives our functions an unexpected effect. Why is that?

Originally, in JavaScript, as long as the new expression after the constructor returned (return) of a primitive type (no return is actually the original type undefined, such as (1)), such as the first (2), Then return the anonymous object created by new, and if the constructor after the new expression returns a reference object, such as Object, function, array, and so on, the returned Reference object overwrites the anonymous object created by new. Now to analyze the writing in (3), because the new string constructs a string reference object, it overwrites the anonymous object created by new, and the reference value of new string is "Jeff Wong", so the pop-up is necessarily the value assigned by the current new string.

Finally, leave a study questions, and let's see what happens when the following code returns:

Copy Code code as follows:

var func = new function () {var str = new String ("Jeff Wong"); return str;};//another way.
Alert (typeof (Func)); Object expected
Alert (func); Guess what this is supposed to be?

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.