Created
November 11, 2016 00:48
-
-
Save victorvhpg/cdb39eb41d0c07488afe914a032eb0ec 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
| function Retangulo(largura, altura) { | |
| this.largura = largura; | |
| this.altura = altura; | |
| } | |
| Retangulo.prototype.getArea = function() { | |
| return this.largura * this.altura; | |
| }; | |
| function Quadrado(lado) { | |
| Retangulo.call(this, lado, lado); | |
| this.algumaCoisa = "xxx"; | |
| } | |
| //faz a herança por prototype com Object.create | |
| Quadrado.prototype = Object.create(Retangulo.prototype, { | |
| constructor: { | |
| value: Quadrado, | |
| enumerable: true, | |
| writable: true, | |
| configurable: true | |
| } | |
| }); | |
| //ou como era antigamente antes do Object.create | |
| var temp = function(){}; | |
| temp.prototype = Retangulo.prototype; | |
| Quadrado.prototype = new temp(); | |
| Quadrado.prototype.constructor = Retangulo; | |
| //instancia | |
| var objQuadradro = new Quadrado(3); | |
| console.log(objQuadradro.getArea()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment