JavaScript advanced Knowledge Point--Length of function

Source: Internet
Author: User

The code information comes from http://ejohn.org/apps/learn/.

How does the length property of a function work?
functionfunction= = 1, "Defines only one parameter"= = 2, "defines two parameters");

It is clear that the length of the function is the number of formal parameters defined.

We can use this to write overloaded functions
functionAddmethod (object, name, fn) {//Store a reference to a past method  varOld =object[name]; //overriding the new methodobject[name] =function(){     //Check the number of defined parameters,    //and we accept the number of receive parameters    if(Fn.length = =arguments.length)//If the match runs this function      returnFn.apply ( This, arguments); //Otherwise, call past methods    Else if(typeofOld = = "function" )       returnOld.apply ( This, arguments); }; }
Take a look at the use of overloaded functions
functionAddmethod (object, name, fn) {//Store a reference to a past method  varOld =object[name]; //overriding the new methodobject[name] =function(){     //Check the number of defined parameters,    //and we accept the number of receive parameters    if(Fn.length = =arguments.length)//If the match runs this function      returnFn.apply ( This, arguments); //Otherwise, call past methods    Else if(typeofOld = = "function" )       returnOld.apply ( This, arguments); }; } functionNinjas () {varNinjas = ["Dean Edwards", "Sam Stephenson", "Alex Russell" ]; Addmethod ( This, "Find",function(){     returnNinjas;   }); Addmethod ( This, "Find",function(name) {varRET = [];  for(vari = 0; i < ninjas.length; i++ )       if(Ninjas[i].indexof (name) = = 0) Ret.push (Ninjas[i]); returnret;   }); Addmethod ( This, "Find",function(First, last) {varRET = [];  for(vari = 0; i < ninjas.length; i++ )       if(Ninjas[i] = = (First + "" +Last ))     Ret.push (Ninjas[i]); returnret; }); }  varNinjas =NewNinjas (); Console.log (Ninjas.find ()); Console.log (Ninjas.find ("Sam")); Console.log (Ninjas.find ("Dean", "Edwards")); Console.log (Ninjas.find ("Alex", "X", "Russell"));

When instantiating Ninja, the method was executed 4 times to ninja, each time covering the Find attribute, depending on the scope chain of the closure, the new method can still refer to Addmethod Old,old the previous method.

The last find is the method that determines whether the parameter is the same as the number of formal parameters, which is the execution, not the call to old. So repeated.

Equivalent to the following code
varNinjas ={find:function(){        varNinjas = ["Dean Edwards", "Sam Stephenson", "Alex Russell"]         varOld =function(){            varNinjas = ["Dean Edwards", "Sam Stephenson", "Alex Russell"]             varOld =function(){                varNinjas = ["Dean Edwards", "Sam Stephenson", "Alex Russell"]                 varOld ; varfn =function(){                     returnNinjas; }                if(Fn.length = =arguments.length) {returnFn.apply ( This, arguments); }Else if(typeofOld = = "function" ) {                  returnOld.apply ( This, arguments);            }            }; varfn =function(name) {varRET = [];  for(vari = 0; i < ninjas.length; i++ )                   if(Ninjas[i].indexof (name) = = 0) Ret.push (Ninjas[i]); returnret; }            if(Fn.length = =arguments.length) {returnFn.apply ( This, arguments); }Else if(typeofOld = = "function" ) {              returnOld.apply ( This, arguments); }        }        varfn =function(First, last) {varRET = [];  for(vari = 0; i < ninjas.length; i++ )               if(Ninjas[i] = = (First + "" +Last ))             Ret.push (Ninjas[i]); returnret; }        if(Fn.length = =arguments.length) {returnFn.apply ( This, arguments); }Else if(typeofOld = = "function" ) {          returnOld.apply ( This, arguments); }},}console.log (Ninjas.find ()); Console.log (Ninjas.find ("Sam")); Console.log (Ninjas.find ("Dean", "Edwards")); Console.log (Ninjas.find ("Alex", "X", "Russell"));

JavaScript advanced Knowledge Point--Length of function

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.