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