Skip to content

Instantly share code, notes, and snippets.

@TiagoWinehouse
Created September 12, 2015 14:46
Show Gist options
  • Save TiagoWinehouse/7e5375b2465473d963d8 to your computer and use it in GitHub Desktop.
Save TiagoWinehouse/7e5375b2465473d963d8 to your computer and use it in GitHub Desktop.
Exemplo: Usando call para encadear construtores para um objeto
function Produto(nome, preco){
this.nome = nome;
this.preco = preco;
if(preco < 0){
throw RangeError('Cannot create product ' + this.nome + ' with a negative price');
}
return this;
}
function Prato(nome, preco){
Produto.call(this, nome, preco);
this.categoria = 'prato';
}
Prato.prototype = Object.create(Produto.prototype);
function Toy(nome, preco){
Produto.call(this, nome, preco);
this.categoria = 'toy';
}
Toy.prototype = Object.create(Produto.prototype);
var cheese = new Prato('feta', 5);
var fun = new Toy('robot', 40);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment