Skip to content

Instantly share code, notes, and snippets.

@wfuertes
Last active December 11, 2015 01:58
Show Gist options
  • Save wfuertes/4527224 to your computer and use it in GitHub Desktop.
Save wfuertes/4527224 to your computer and use it in GitHub Desktop.
//Criando uma função construtora para simular uma classe: Person.
function Person(options) {
//attributos públicos
this.name = options.name;
this.gender = options.gender;
//métodos públicos
this.speak = function() {
console.log(this.name + ' sabe falar!');
};
this.walk = function() {
console.log(this.name + ' sabe andar!');
};
}
//Usando composição de Person, ao criadar a "classe" Employe.
function Employe(options) {
//attributos públicos
this.person = new Person(options);
this.register = options.register;
//método público
this.salary = function() {
if (this.person.gender === 'M') {
console.log("Male's salary");
}else {
console.log("Female's salary");
}
};
}
//Criando uma instância da "classe" Person
var person = new Person({name: 'Pedro', gender: 'M'});
person.speak();
person.walk();
//Criando uma instância da "classe" Employe
var male = new Employe({name: 'Fred', gender: 'M'});
male.person.speak();
male.person.walk();
male.salary();
//Criando outra instância da "classe" Employe
var female = new Employe({name: 'Linda', gender: 'F'});
female.person.speak();
female.person.walk();
female.salary();
/*
Saida no console:
>> Pedro sabe falar!
>> Pedro sabe andar!
>> Fred sabe falar!
>> Fred sabe andar!
>> Male's salary
>> Linda sabe falar!
>> Linda sabe andar!
>> Female's salary
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment