Created
May 30, 2017 13:49
-
-
Save brunoczim/5edf9664e9e60b2decb904cb7c40692e to your computer and use it in GitHub Desktop.
A simple snippet for reducing code when extending prototypes .
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.prototype.extends = function(Parent) { | |
this.prototype = Object.create(Parent.prototype); | |
this.prototype.constructor = this; | |
}; | |
//Now | |
/* | |
function Rectangle(w, h) { | |
this.width = w; | |
this.height = h; | |
} | |
Rectangle.prototype.getArea = function() { | |
return this.height * this.width; | |
} | |
function Square(size) { | |
Rectangle.call(this, size, size); | |
} | |
Square.extends(Rectangle); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment