Created
October 10, 2013 11:41
-
-
Save francoisblarel/6917050 to your computer and use it in GitHub Desktop.
Method-overloading in javascript (from "Secrets of the Javascript ninja" book)
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
| 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); | |
| }; | |
| } | |
| var ninjas = { | |
| values: ["Dean Edwards", "Sam Stephenson", "Alex Russell"] | |
| }; | |
| addMethod(ninjas, "find", function(){ | |
| return this.values; | |
| }); | |
| addMethod(ninjas, "find", function(name){ | |
| var ret = []; | |
| for (var i = 0; i < this.values.length; i++) | |
| if (this.values[i].indexOf(name) == 0) | |
| ret.push(this.values[i]); | |
| return ret; | |
| }); | |
| addMethod(ninjas, "find", function(first, last){ | |
| var ret = []; | |
| for (var i = 0; i < this.values.length; i++) | |
| if (this.values[i] == (first + " " + last)) | |
| ret.push(this.values[i]); | |
| return ret; | |
| }); | |
| assert(ninjas.find().length == 3, "Found all ninjas"); | |
| assert(ninjas.find("Sam").length == 1, "Found ninja by first name"); | |
| assert(ninjas.find("Dean", "Edwards").length == 1, "Found ninja by first and last name"); | |
| assert(ninjas.find("Alex", "Russell", "Jr") == null, "Found nothing"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment