Skip to content

Instantly share code, notes, and snippets.

@valdiney
Created April 28, 2014 00:14
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 valdiney/11358746 to your computer and use it in GitHub Desktop.
Save valdiney/11358746 to your computer and use it in GitHub Desktop.
Estudando Herança em javascript através de funções construtoras...
//////////////////////////////////////////////////
// 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