Function in js, js function
The function object of js has an arguments attribute during the call process. It is created by the script interpreter (this is the only way to create arguments ). The arguments attribute can be viewed as an Array object. It has the length attribute. It can access each parameter through the serial number and obtain reference to the Function object being executed through the callee attribute of argument. As follows:
function factorial(n){ if(n<=n){ return 1; }else{ return n*arguments.callee(n-1); }}alert(factorial(5));
The preceding uses the callee attribute to complete a recursive algorithm.
Another property of Function is caller, which points to the parent Function object that is calling the current Function. Using the callee and caller attributes, you can easily traverse the stack, for example:
Function fool (v1) {foo2 (v1, v2, v3);} function foo2 (v1, v2) {foo3 (v1, v2, v2 * v2 );} function foo3 (v1, v2, v3) {var foo = argument. callee; while (foo & (foo! = Window) {document. writeln ('<br> call parameters: <br>', '------------------------------- <br>'); var args = foo. argument; argn = args. length; for (var 1 = 0; I <arg; I ++) {document. writeln ('args [', I,']: ', args [I],' <br> ');} document. writeln ('<br>'); foo = foo. caller;} foo (5 );
Function is a special object in js. It is embodied in its multiple identities, such:
// Function as the class declaration and implementation function ClassA () {this. prop1 = "prop1"; this. prop2 = "prop2";} // function as the constructor var obj = new CalssA (); // outputs true. function as the class references alert (obj instanceof CalssA );
A Function can declare a common Function. This is the same concept as other languages. However, a Function can also be used for class declaration and implementation, object constructors, and class references. The above Code declares the ClassA class through the function keyword, and declares two attributes prop1 and prop2 through the this keyword. Then, when creating the obj object, ClassA () it starts from the role of object constructors. Finally, the instanceof keyword is used in the code to determine whether the obj object is an instance of the ClassA class. At this time, ClassA plays a role in class reference.
The question about function functions in Javascript is recently viewed by js.
People is a Function object, and its attributes can be extended.
In js, many objects can continue to expand attributes, except for native objects.
For example:
Var num1 = new Number ("111 ");
Num1.prop1 = "aaa ";
Alert (num1.prop1); // you can call it out.
Var num2 = 111;
Num2.prop1 = "aaa ";
Alert (num2.prop1); // cannot be typed, because num2 is native
Var str1 = "str ";
Str1.prop1 = "aaa ";
Alert (str1.prop1); // cannot be typed, because str1 is native
Var str2 = new String ("str ");
Str2.prop1 = "aaa ";
Alert (str2.prop1); // you can call it out because it is new.
Var array = [];
Array. prop1 = "aaa ";
Alert (array. prop1); // can be typed out, because array = [] is short for array = new Array.
Likewise:
Function People (name) {alert ('ss')} Is var People = new Function ("name", "alert ('ss ')");
Since it is new, you can add attributes at will.
Functions and new functions in JS
Document. body. onload = function (xxx)
Document. body. onload = new function (xxx)
Document. body. onload = function () {xxxxxxxxx}
First, the first type:
Do not write functions (xxx). Otherwise, everyone thinks it is a function keyword. It is actually a custom function.
As for the following method, the effect is the same, but note that there is a new keyword, and it is not a function, but a Function, for example:
Document. onmouseup = new Function ("flag = true ");
The third is the most common syntax:
Function () {statement}
-----------------------------------
I.
Var foo01 = function (){
Var temp = 100;
This. temp = 200;
Return temp + this. temp;
}
The foo01 function is reloaded here. The effect is similar to that of function foo01 () {statement}, but the difference is that var foo01 = function () {statement} reinstalls the foo01 function, which is equivalent to a model, there are still some differences. For example, a function calls the foo01 function written in these two ways, but the method in this example inherits the foo01 method, but function foo01 () {} This write law directly executes the function, so both write methods are very useful.
II.
Var foo02 = new function ()
{
Var temp = 100;
This. temp = 200;
Return temp + this. temp;
}
This method is not common, but it is similar to the example one, except that the keyword new is added. Obviously, this function must first define the model of the custom function, this function can then be instantiated using the new keyword.
III.
Var foo3 = new Function ('var temp = 100; this. temp = 200; return temp + this. temp ;');
As mentioned above.