The
function is a very important language element in JavaScript, providing 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 Results:
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;
return temp + 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 : 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 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 : var Foo4 = Function (' var temp = This.temp =; return temp + 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 no new to generate any side effects, which Also embodies one of the biggest features of javascript: flexibility. Can save the province.