JavaScript new Function (") and new function () {} instance

Source: Internet
Author: User
Tags anonymous constructor object object

Scenario One:

The code is as follows Copy Code
var yx01 = new function () {return "center"};
alert (YX01);

We run the story one code, and it returns "[Object Object]", which is equivalent to:

The code is as follows Copy Code
function Anonymous class () {
return "center";
}
var yx01 = new anonymous Class ();
alert (YX01);

Our code for scenario one is modified below:

The code is as follows Copy Code
var yx01 = new function () {return new String ("Center")};
alert (YX01);

We run and will find the return of the "center", which is why?
As soon as the constructor returns (return) a Reference object (array, object, function, etc.) to the new expression, it overrides the anonymous object created by new, and returns a primitive type (return) of the original type undefined), then returns the anonymous object created by new. (Thank you Lunatic_sun, describe more precise points)
Because the new string constructs an object, rather than a String, and the new string (x) takes an argument, then alert it returns x. So yx01 returns the object of the new String ("center"), and alert yx01 displays the "center of the circle."
Scenario Two:

The code is as follows Copy Code
var yx02 = function () {return "center"} ();
alert (yx02);

We run the story code and return to the "center of the Circle," which is equivalent to:

The code is as follows Copy Code
var anonymous functions = function () {return "center"};
yx02 = anonymous function ();
alert (yx02);

Obviously, YX02 returns the execution result value of the anonymous function, that is, the yx02 is: "Circle


instance

The code is as follows Copy Code

<! DOCTYPE html>
    <meta charset= "Utf-8"/>
    <title></title>
<body>
    < Script>
        var f = new Function (' x ', ' y ', ' return x + y; '); /equivalent to var f = function (x, y) {return x + y;}
        Console.log (f (1, 1));
        var str = ' {' id ': 108} ';
        Console.log ((New Function (' return ' + str)) ());//String to object

        f = new function () {return ' CA ';};
        Console.log (f);
       //equivalent
       /*function anonymous class () {
            return ' CA ';
       }
        f = new anonymous Class ();
        Console.log (f); */
        F = new function () {return new String (' Ca ');
        Console.log (f);
       //As soon as the new expression constructor Returns (return) a Reference object (array, object, function, etc.), Will overwrite the anonymous object created by new, if returning an original type (return is in fact the original type undefined), then return the anonymous object created by new
    </ Script>
</body>

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.