Home >Web Front-end >JS Tutorial >Analyze several simple and interesting pieces of code through anonymous functions of javascript_javascript skills

Analyze several simple and interesting pieces of code through anonymous functions of javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:24:26876browse

1. Simple form of encapsulation call

Copy code The code is as follows:

var userName = function( ) { return "jeff wong" } ();
alert(userName);


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

var anonymousFunc = function() { return "jeff wong" }; // Anonymous function
var name = anonymousFunc(); //Execute this function to return the name of the person
alert(name);


2. New is the form of Function (uppercase Function)
Copy code 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, we new an Object, the variable a pops up is [object Object], and new a Function (note that it is capitalized Function), b is in When it pops up, an anonymous function is generated. Since b is an anonymous function, the function can of course be executed. We can continue to try the following code to verify our guess:
Copy code The code is as follows:

alert(b()); //undefined
alert(a()); //Script error prompts "Missing function"



3. New function can make a difference (lowercase function)
(1), simple empty function
Copy code 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 Writing method:
Copy code The code is as follows:

function anonymousClass() { } //Anonymous Class
var instance = new anonymousClass();
alert(typeof (instance));//object
alert(instance); //[object Object]

[code]

(2). The function has a return value, which 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 The code is equivalent to the following writing:
Copy the code 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), the function still has a return value, the writing method is slightly different

Please pay attention to the difference between the following code and (2) , because what we want to focus on next is the little bit of different writing forms:
Copy code The code is as follows:

var func = new function() { return new String("jeff wong"); };
alert(typeof (func)); //object expected
alert(func); //Here?!
//alert(func()); //Script error missing function



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

function anonymousClass() { return new String("jeff wong"); }
var instance = new anonymousClass();
alert(typeof (instance));
alert(instance);


Have you run it and seen the results? That's right, in the third way of writing, when we pop up func or instance, we unexpectedly get a string "jeff wong". Comparing the codes in (2) and (3) carefully, except for the slightly different writing method of return, the two codes are almost identical, so we infer that there is no doubt that it is the form of new String that makes our function generate Unexpected effect. Why is this happening?

It turns out that in JavaScript, as long as the constructor after the new expression returns (return) a primitive type (when there is no return, it actually returns the primitive type undefined, such as (1)), such as (2) Writing method, then the anonymous object created by new is returned; and if the constructor after the new expression returns a reference object, such as an object (Object), a function (function), an array (Array), etc., then the returned reference object will be Overwrite the anonymous object created by new. Now let’s analyze the writing in (3). Since new String will construct a string reference object, it covers the anonymous object created by new, and the reference value pointed to by new String is “jeff wong”, so the pop-up is inevitable. Is the value assigned by the current new String.

Finally, leave a question to think about. Let’s see what the following code returns:

Copy code The code is as follows:

var func = new function() { var str = new String("jeff wong"); return str; };//Write it another way
// alert(typeof (func)); //object is expected
alert(func); //Guess what the result should be here?

Author: Jeff Wong
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn