javascript method overloading

javascript中的方法重载实现:

// addMethod - By John Resig (MIT Licensed)function addMethod(object, name, fn) {    var old = object[name];    if (old) {        object[name] = function() {            // fn.length: 是指方法fn的参数个数            if (fn.length == arguments.length) {                return fn.apply(this, arguments);            } else if (typeof old == 'function') {                return old.apply(this, arguments);            }        };    } else {        object[name] = fn;    }}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});var users = new Users();users.find(); // Finds allusers.find("John"); // Finds users by nameusers.find("John", "Resig"); // Finds users by first and last nameusers.find("John", "E", "Resig"); // Finds all

References

  1. John Resig