Scenario 1:
VaR yx01 = new function () {return "center"}; alert (yx01 );
We run scenario 1 code and will return"[Object object]", The code is equivalent:
Function Anonymous class () {return "center";} var yx01 = new anonymous class (); alert (yx01 );
The Code of scenario 1 is transformed as follows:
VaR yx01 = new function () {return new string ("center")}; alert (yx01 );
When we run the command, we will find that the returned result is the "Center of the circle". Why?
As long as the constructor after the new expression returns (return) a referenced object (array, object, function, etc.), it will overwrite the anonymous object created by the new expression. If return is returned (return) an original type (undefined is actually the original type of return when no return is required). Then, an anonymous object created by new is returned. (Thank you, lunatic_sun. The description is more accurate)
Because New String constructs an object instead of a string, and if new string (x) carries a parameter, alert returns X. Therefore, yx01 returns the new string ("Circle Center") object, while alert yx01 displays "circle center ".
Scenario 2:
VaR yx02 = function () {return "center"} (); alert (yx02 );
We run the scenario 2 code and will return"Center", The code is equivalent:
VaR anonymous function = function () {return "center"}; yx02 = anonymous function (); alert (yx02 );
Obviously, yx02 returns the execution result value of the anonymous function, that is, yx02 is the center of the circle ".
Of course, the execution result of an anonymous function can also be an anonymous object. For more information about common applications, see JavaScript module mode.