Created
April 28, 2014 00:14
-
-
Save valdiney/11358746 to your computer and use it in GitHub Desktop.
Estudando Herança em javascript através de funções construtoras...
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
////////////////////////////////////////////////// | |
// Estudando Herança em javascript através de funções construtoras... | |
// Estudante: Valdiney França... | |
///////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////// | |
// Função construtora, uma simulação de uma Class Abstrata... | |
function Animal() {/*...*/} | |
////////////////////////////////////////////////////////////// | |
// Metodos da Class... | |
Animal.prototype.nascer = function() { | |
return 'Nasceu um Animal.'; | |
} | |
Animal.prototype.respirar = function() { | |
return 'Começou a respirar por se mesmo.'; | |
} | |
Animal.prototype.morrer = function() { | |
return 'Acabou de morrer.'; | |
} | |
////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////// | |
// Função construtora de um Gato... | |
function Gato( nome ) { | |
this.nome = nome; | |
} | |
////////////////////////////////////////////////////////////// | |
// A class Gato está estendendo a class Animal... | |
Gato.prototype = new Animal(); | |
Gato.prototype.constructor = Gato; | |
////////////////////////////////////////////////////////////// | |
// Acrescentando um método miar a class Gato... | |
Gato.prototype.miar = function() { | |
return this.nome + ' está miando.'; | |
} | |
/////////////////////////////////////////////////////////////////////////////////// | |
// Instanciando um objeto do tipo Gato e usando alguns métodos da class Animal... | |
var gatinho = new Gato('Belinha'); | |
console.log( gatinho.nascer() ); | |
console.log( gatinho.respirar() ); | |
console.log( gatinho.miar() ); | |
/////////////////////////////////////////////////////////////////////////////////// | |
// O gatinho morre depois de 3 Mille segundos... | |
var CeifarVida = window.setInterval( function() { | |
console.log( gatinho.morrer() ); | |
}, 3000 ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment