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> |