Created
June 24, 2015 18:44
-
-
Save danilodeveloper/dd2d0b383bc7287d7c32 to your computer and use it in GitHub Desktop.
Uso de prototype e new no Javascript
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
function Pessoa(nome) { | |
if(!this instanceof Pessoa) { | |
return new Pessoa(nome); | |
} | |
this.nome = nome; | |
} | |
Pessoa.prototype.getNome = function() { | |
return this.nome; | |
} | |
Pessoa.prototype.getSobrenome = function() { | |
return this.sobrenome; | |
} | |
Pessoa.prototype.setSobrenome = function(sobrenome) { | |
this.sobrenome = sobrenome; | |
} | |
Pessoa.prototype.getNomeCompleto = function() { | |
return this.getNome() + ' ' + this.sobrenome; | |
} | |
var pessoa1 = new Pessoa('Marcos'); | |
var pessoa2 = new Pessoa('Isabel'); | |
pessoa1.setSobrenome('Cesar Barsotti'); | |
console.log(pessoa1.getNomeCompleto()); | |
pessoa2.setSobrenome('Xavier Barsotti'); | |
console.log(pessoa2.getNomeCompleto()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment