John Resig, has explained us a better and interesting way of Method Overloading. Here he has discussed about the ability to run different code depending on the number of arguments passed into the same function. For those who are not familiar with, it’s just a way of mapping a single function call to multiple functions based on arguments they accept.
Function:
// addMethod - By John Resig (MIT Licensed)
function addMethod(object, name, fn){
var old = object[ name ];
object[ name ] = function(){
if ( fn.length == arguments.length )
return fn.apply( this, arguments );
else if ( typeof old == 'function' )
return old.apply( this, arguments );
};
}Using It:
function Users(){
addMethod(this, "find", function(){
// Find all users...
});
addMethod(this, "find", function(name){
// Find a user by name
});
addMethod(this, "find", function(first, last){
// Find a user by first and last name
});
}Using object prototype:
function Users(){}
addMethod(Users.prototype, "find", function(){
// Find all users...
});
addMethod(Users.prototype, "find", function(name){
// Find a user by name
});
addMethod(Users.prototype, "find", function(first, last){
// Find a user by first and last name
});End Result:
var users = new Users();
users.find(); // Finds all
users.find("John"); // Finds users by name
users.find("John", "Resig"); // Finds users by first and last name
users.find("John", "E", "Resig"); // Does nothingHope the above will help developers a lot!
Rock Up the Web World!!!
Related Entries...
AIR is rocking in the web world, if you guys! Search for Adobe AIR you will get tons of web snippets ...
Hey folks, hmmm... How can I start.... !!! Yeah, well folks... below is very simple snippet code for ...
I am back again with some helpful snippet code. Here using JavaScript we can pro actively sort date f ...
Making a preloader in flash is very simple. By using ProgressBar and Image Loader components, we can ...
Hi Dev Folks, this topic explains you about different patterns used to match character combinations i ...
The Sprite class is new in ActionScript 3.0, This class is a basic display list building block, which ...
You guys might know the importance of XML, XML (Extensible Markup Language) is a flexible markup lang ...
Below is the code which is written in JavaScript, we can use this script for bookmarking the particul ...
Hi Guys, I would like to share a JavaScript snippet code which will remove special characters (like ! ...
As we all know AJAX is rocking the Web World all around, just I had a thought to developer a simple A ...























One Response
[...] stillisstillmoving.com wrote an interesting post today!.Here’s a quick excerpt John Resig, has explained us a better and interesting way of Method Overloading. Here he has discussed about the ability to run different code depending on the number of arguments passed into the same function. For those who are not familiar with, it’s just a way of mapping a single function call to multiple functions based on arguments they accept. Function: // addMethod – By John Resig (MIT Licensed) function addMethod(object, name, fn){ var old = object[ name ]; object[ name ] = function(){ [...]