Created
June 30, 2012 05:51
-
-
Save renatoargh/3022529 to your computer and use it in GitHub Desktop.
This file contains 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
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