'JavaScriptMethodOverloading'에 해당되는 글 1건

  1. 2009.12.21 간단한 JavaScriptMethodOverloading
반응형
// 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);
    };
}

// Now setup the methods
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
    });
}

// Now use the methods
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 nothing



출처 : http://openframework.or.kr/Wiki.jsp?page=JavaScriptMethodOverloading
Posted by 1010