new function () Considerations in JavaScript

Source: Internet
Author: User
Tags anonymous constructor variable scope
The code is as follows Copy Code


Varyx01=new function () {
Return "center"
};
alert (yx01);//[objectobject]

Equivalent:

The code is as follows Copy Code

function Anonymous class () {
return "center";
}
Varyx01=new anonymous Class (); alert (YX01);

The following conditions are different:

The code is as follows Copy Code

Varyx01=new function () {
Returnnewstring ("center")
};
alert (yx01);/center

Description: 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 return is a primitive type (returns are in fact the original type undefined), then return the anonymous object created by new.


Let's refer to a MDN explanation to see what the new operator does:

Whenthecodenewfoo (...) Isexecuted,thefollowingthingshappen:
1.anewobjectiscreated,inheritingfromfoo.prototype.
2. Theconstructorfunctionfooiscalledwiththespecifiedargumentsandthisboundtothenewlycreatedobject.newfooisequivalenttonewfoo (), i.e.ifnoargumentlistisspecified,fooiscalledwithoutarguments.
3. THEOBJECTRETURNEDBYTHECONSTRUCTORFUNCTIONBECOMESTHERESULTOFTHEWHOLENEWEXPRESSION.IFTHECONSTRUCTORFUNCTIONDOESN ' Texplicitlyreturnanobject,theobjectcreatedinstep1isusedinstead. (Normallyconstructorsdon ' Treturnavalue, Buttheycanchoosetodosoiftheywanttooverridethenormalobjectcreationprocess.)

Add: function,new function,new function

Use method One:

The code is as follows Copy Code

Varfoo01=function ()//orfun01=function ()
{
vartemp=100;
this.temp=200;
Returntemp+this.temp;
}

Alert (typeof (FOO01));
Alert (foo01 ());
Run Result:
function
300

The most common function is to use the method, which is to set a JavaScript functions. The two kinds of writing show the same effect, the only one is that the latter is a higher initialization priority. In a variable scope within a large expansion number, this refers to the owner of the Foo01, the Window object.

Use Method Two:

The code is as follows Copy Code

Varfoo02=new function ()
{
vartemp=100;
this.temp=200;
Returntemp+this.temp;
}

Alert (typeof (Foo02));
Alert (Foo02.constructor ());
Run Result:
Object
300

This is a comparison of the use of puzzle function, as if to set a function. But this is actually a user-defined object in JavaScript, but this is an anonymous class. This usage has nothing to do with the use of the function itself, which constructs a variable scope in the large extension, this refers to the scope itself.

Use method Three:

The code is as follows Copy Code

varfoo3=new function (' vartemp=100;this.temp=200;returntemp+this.temp; ');

Alert (typeof (Foo3));
Alert (Foo3 ());
Run Result:
function
300

Use the System built-in function object to construct a function, which is identical to the first way in method one in terms of both effect and initialization precedence, which is given by the function body in string form.

Use method Four:

The code is as follows Copy Code

Varfoo4=function (' vartemp=100;this.temp=200;returntemp+this.temp; ');

Alert (typeof (Foo4));
Alert (Foo4 ());
Run Result:
function
300

This is not often used, the effect is the same as the method three, but it is not clear that there is no new to generate any side effects, which also embodies the largest feature of javascript: Flexible! Can save the province.

If you don't understand the difference between two implementations of the JS class to define the prototype method

  code is as follows copy code

Jscriptclass :
Functionjsclass ()
{

}

Extendsprototypemethod:
jsclass.prototype.methoda=function ()
{

};

Or

Function=jsclass.prototype.methoda ()
{

 

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.