Skip to content

Instantly share code, notes, and snippets.

@Sljubura
Last active December 13, 2015 22:59
Show Gist options
  • Select an option

  • Save Sljubura/4988684 to your computer and use it in GitHub Desktop.

Select an option

Save Sljubura/4988684 to your computer and use it in GitHub Desktop.
Pretty prototype
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