Skip to content

Instantly share code, notes, and snippets.

@renatoargh
Created June 30, 2012 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renatoargh/3022529 to your computer and use it in GitHub Desktop.
Save renatoargh/3022529 to your computer and use it in GitHub Desktop.
var prototipo = {
id: 0,
nome: '',
idade: 0,
constante: 3.14 //Essa linha é nova
};
var construirPessoa = function(id, nome, idade){
var novo = Object.create(prototipo);
novo.id = id;
novo.nome = nome;
novo.idade = idade;
return novo;
};
var renato = construirPessoa(1, 'Renato Gama', 25);
//Nesta linha alteramos o protótipo para
//exemplificar a situação apresentada anteriormente:
//PROTÓTIPO ALTERADO DEPOIS DE UMA INTÂNCIA TER SIDO GERADA
prototipo.constante = 3.1415;
prototipo.idade = 1;
console.log(renato.idade); //loga 25, pois a linha anterior altera apenas o valor padrão do protótipo
console.log(renato.constante); //loga 3.1415
console.log(renato.__proto__.id); //loga 0
console.log(renato.__proto__.nome); //loga ''
console.log(renato.__proto__.idade); //loga 1
console.log(renato.__proto__.constante); //loga 3.1415
console.log(renato.euNaoFuiDefinido); //loga 'undefined'
console.log(renato.toString()); //loga '[object Object]'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment