Last active
December 13, 2015 22:59
-
-
Save Sljubura/4988684 to your computer and use it in GitHub Desktop.
Pretty prototype
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
| if (typeof Function.prototype.method !== "function") { // Check if method is already implemented | |
| Function.prototype.method = function (name, implementation) { | |
| this.prototype[name] = implementation; // Add implementation to prototype | |
| return this; | |
| }; | |
| } | |
| // Pretty prototype | |
| var User = function (name) { | |
| this.name = name; | |
| }. | |
| method('getName', function () { | |
| return this.name; | |
| }). | |
| method('setName', function (name) { | |
| this.name = name; | |
| return this; | |
| }); | |
| var first = new User('Adam'); | |
| console.log(first.getName()); // WebStorm suggests getName() :O | |
| console.log(first.setName('Jaime').getName()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment