function, new function, the difference between the new function

Source: Internet
Author: User
Tags class definition variable scope
http://www.jb51.net/article/7955.htm


A function is a very important language element in JavaScript and provides a function keyword and a built-in object function, and the following are the possible uses and relationships between them.

Use method One:


var foo01 = function ()//or Fun01 = function ()
{
var temp = 100;
This.temp = 200;
return temp + this.temp;
}

Alert (typeof (FOO01));
Alert (foo01 ());
Run Result:
function
300 the most common function usage, set a JavaScript function. 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:


Var foo02 = new function ()   
 {  
      var temp = 100;  
     this.temp =  200; &NBSP
     return temp + this.temp;  
 } &NBSP

 alert (typeof (Foo02))   
 alert (Foo02.constructor ());    &NBSP
Run results:  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. &NBSP

     use method: 


var Foo3 = new Function (' var temp = This.temp =; return temp + 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 in method one in terms of both effect and initialization precedence, that is, the function body is given in string form.

Use method Four:

var Foo4 = Function (' var temp = This.temp =; return temp + this.temp; ');

Alert (typeof (Foo4));
Alert (Foo4 ());
Run Result:
function
300 This method is not commonly used, the effect and method of the same, but it is not clear that no new to generate any side effects, which also embodies the most features of javascript: flexible. Can save the province.

On the question of function initialization precedence, see the reply to "the difference between two implementations of the JS class definition prototype method."




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.