JS's function object has a arguments property during invocation, which is created by the script interpreter (which is also the only way to create arguments). The arguments property can be thought of as an array object with the Length property, each parameter can be accessed by an ordinal, and a reference to the executing function object can be obtained through the argument callee property. As follows:
function factorial (n) { if (n<=n) { return 1; } else{ return N*arguments.callee (n-1); }} Alert (factorial (5));
The above uses the Callee property to complete a recursive algorithm.
Another property of function is caller, which points to the parent function object that is calling the current function. With the callee and caller properties, you can easily implement a traversal of the stack, such as:
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 parameter:<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, which is embodied in his multiple identities, such as:
function as the Declaration and implementation of the class function ClassA () { this.prop1= "Prop1"; This.prop2= "Prop2";} function as a constructor var obj=new calssa ();//output true,function as Class reference alert (obj instanceof Calssa);
function can declare ordinary functions, which are the same as the concepts of other languages, but functions can also be used for the Declaration and implementation of classes, the constructors of objects, and references to classes. The ClassA class is declared by the function keyword in the code above, and two properties Prop1 and PROP2 are declared with the This keyword, and then ClassA () is the object constructor when creating the Obj object. The final code uses the INSTANCEOF keyword to determine if the Obj object is an instance of the ClassA class, at which point the ClassA acts as a class reference.