Created
August 29, 2010 01:03
-
-
Save devongovett/555790 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#this is the coffeescript with method overloading | |
class User | |
find: (id) -> | |
console.log "find by id" | |
find: (first, last) -> | |
console.log "find by first and last name" | |
class NewUser extends User | |
find: (first, middle, last) -> | |
console.log "find by full name" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//and this is the compiled JavaScript | |
//unfortunately, because of inheritance, __addMethod must be called for | |
//all methods being added to any class unless the compiler is smart enough | |
//to keep track of the methods of the super class and whether they are | |
//overloaded. | |
var NewUser, User; | |
var __extends = function(child, parent) { | |
var ctor = function(){}; | |
ctor.prototype = parent.prototype; | |
child.prototype = new ctor(); | |
child.prototype.constructor = child; | |
if (typeof parent.extended === "function") parent.extended(child); | |
child.__super__ = parent.prototype; | |
}; | |
//originally by John Resig | |
var __addMethod = function(klass, name, fn) { | |
var old = klass.prototype[name]; | |
klass.prototype[name] = !(name in klass.prototype) ? fn : function() { | |
if(fn.length == arguments.length) | |
return fn.apply(this, arguments); | |
else if(typeof old == 'function') | |
return old.apply(this, arguments); | |
}; | |
}; | |
User = function() {}; | |
__addMethod(User, "find", function(id) { | |
console.log("find by id"); | |
}); | |
__addMethod(User, "find", function(first, last) { | |
console.log("find by first and last name"); | |
}); | |
NewUser = function() { | |
return User.apply(this, arguments); | |
}; | |
__extends(NewUser, User); | |
__addMethod(NewUser, "find", function(first, middle, last) { | |
console.log("find by full name"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment