Skip to content

Instantly share code, notes, and snippets.

@victorvhpg
Created November 11, 2016 00:48
Show Gist options
  • Select an option

  • Save victorvhpg/cdb39eb41d0c07488afe914a032eb0ec to your computer and use it in GitHub Desktop.

Select an option

Save victorvhpg/cdb39eb41d0c07488afe914a032eb0ec to your computer and use it in GitHub Desktop.
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