In John Resig tutorial on Javascript , there's a code used for function overloading, which I couldn't figure out how it works :
function addMethod(object, name, fn){
// Save a reference to the old method
var old = object[ name ];
// Overwrite the method with our new one
object[ name ] = function(){
// Check the number of incoming arguments,
// compared to our overloaded function
if ( fn.length == arguments.length )
// If there was a match, run the function
return fn.apply( this, arguments );
// Otherwise, fallback to the old method
else if ( typeof old === "function" )
return old.apply( this, arguments );
};
}
function Ninjas(){
var ninjas = [ "Dean Edwards", "Sam Stephenson", "Alex Russell" ];
addMethod(this, "find", function(){
return ninjas;
});
addMethod(this, "find", function(name){
var ret = [];
for ( var i = 0; i < ninjas.length; i++ )
if ( ninjas[i].indexOf(name) == 0 )
ret.push( ninjas[i] );
return ret;
});
addMethod(this, "find", function(first, last){
var ret = [];
for ( var i = 0; i < ninjas.length; i++ )
if ( ninjas[i] == (first + " " + last) )
ret.push( ninjas[i] );
return ret;
});
}
addMethod
doesn't over write the first one ? old
? Both of your questions answer one another. The second addMethod
does overwrite the first find
with a closure , that thanks to the old
object containing the first find
, is able to fall back to it in case the 2nd find
mismatches the argument count.
When the third call to addMethod
is done, it's basically doing the same thing, recursively . So it overwrites the closure with another closure, where old
is the object of which the function find
is a closure with the 2nd find and the old old
object containing the 1st find
. Phew.
Here's an approximate graphic representation:
find -> [ find3 ]
[ old2 --]--> [ find2 ]
[ old1 --]--> [ find1 ]
[ null ]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.