[Functions] function, new function, New Function

Source: Internet
Author: User
Tags variable scope

From:Http://fengsky491.javaeye.com/blog/228420

 

A function is a very important language element in JavaScript and provides a function keyword and a built-in object function. The following describes its possible usage and the relationship between them.

Method 1:

VaR foo01 = function () // or fun01 = function ()
{
VaR temp = 100;
This. Temp = 200;
Return temp + this. Temp;
}

Alert (typeof (foo01 ));
Alert (foo01 ());


Running result:
Function
300
The most common function usage method is to define a JavaScript function. The two statements have the same running effect, but the only one is that the latter one has a higher initialization priority. In the variable scope of the expanded number, this refers to the owner of foo01, that is, the window object.

Method 2:

VaR foo02 = new function ()
{
VaR temp = 100;
This. Temp = 200;
Return temp + this. Temp;
}

Alert (typeof (foo02 ));
Alert (foo02.constructor ());

Running result:
Object
300
This is a usage of puzzle functions, as if to define a function. But this is actually a User-Defined Object in Javascript, but here it is an anonymous class. This usage has nothing to do with the use of the function itself, and a variable scope will be built in the large expansion number. This refers to the scope itself.

Method 3:

VaR foo3 = new function ('var temp = 100; this. Temp = 200; return temp + this. Temp ;');
Alert (typeof (foo3 ));
Alert (foo3 ());

Running result:
Function
300
Build a function using the built-in function object. This is the same as the first method in method 1 in terms of effect and initialization priority, that is, the function body is given as a string.

Method 4:

VaR foo4 = function ('var temp = 100; this. Temp = 200; return temp + this. Temp ;');

Alert (typeof (foo4 ));
Alert (foo4 ());

Running result:

function
300
this method is not frequently used. The effect and method are similar, but it is unclear whether new is used to generate any side effects. This also reflects one of the biggest features of javascript: flexibility! Save.

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.