Created
October 1, 2013 13:20
-
-
Save edinella/6778334 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// motor | |
function Motor(hp){ | |
this.hp = hp; | |
this.rotacoes = 0; | |
console.log('criou novo motor:', this); | |
} | |
Motor.prototype.liga = function(){ | |
this.rotacoes = 2000; | |
console.log('ligou o motor:', this); | |
}; | |
Motor.prototype.desliga = function(){ | |
this.rotacoes = 0; | |
console.log('desligou o motor:', this); | |
}; | |
Motor.prototype.acelera = function(quanto){ | |
this.rotacoes = this.rotacoes+quanto; | |
console.log('acelerou o motor:', this); | |
}; | |
Motor.prototype.desacelera = function(quanto){ | |
this.rotacoes = this.rotacoes-quanto; | |
console.log('desacelerou o motor:', this); | |
}; | |
// carro | |
function Carro(marca, motor){ | |
this.marca = marca; | |
this.motor = motor; | |
console.log('criou carro:', this); | |
} | |
Carro.prototype.anda = function(){ | |
this.motor.liga(); | |
this.motor.acelera(1000); | |
console.log('andou com o carro:', this); | |
}; | |
Carro.prototype.para = function(){ | |
this.motor.desacelera(1000); | |
this.motor.desliga(); | |
console.log('parou o carro:', this); | |
}; | |
// meu carro | |
console.log('---- Um fusketa ----'); | |
var motorDeFusketa = new Motor(40); | |
var meuCarro = new Carro('Wolkswagen', motorDeFusketa); | |
meuCarro.anda(); | |
meuCarro.para(); | |
console.log('---- Uma lambo ----'); | |
var motorDeLambo = new Motor(520); | |
var meuNovoCarro = new Carro('Lamborghini', motorDeLambo); | |
meuNovoCarro.anda(); | |
meuNovoCarro.para(); | |
console.log('---- o carro de domingo ----'); | |
function Gallardo(){ | |
var marca = 'Lamborghini'; | |
var motor = new Motor(520); | |
Carro.apply(this, [marca, motor]); | |
} | |
Gallardo.prototype = new Carro(); | |
// vamos dar um passeio | |
var carroDeDomingo = new Gallardo(); | |
carroDeDomingo.anda(); | |
carroDeDomingo.para(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment