function functions in JS

Source: Internet
Author: User

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.




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.