Last active
August 29, 2015 14:24
-
-
Save danilodeveloper/784c679216efddd8f08d to your computer and use it in GitHub Desktop.
Herança no Javascript usando new e Object.create
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
// http://jsfiddle.net/0s8kzoke/ | |
console.log('----VEHICLE DEFINITION----'); | |
function Vehicle(name, model, speedLimit) { | |
// prevent the scope error when the programmer forgot the | |
// 'new' keyword before call the constructor. | |
if (!(this instanceof Vehicle)){ | |
return new Vehicle(name, model, speedLimit); | |
} | |
this.name = name; | |
this.model = model; | |
this.speedLimit = speedLimit; | |
this.speed = 0; | |
return this; | |
} | |
Vehicle.prototype.getName = function getName() { | |
return this.name; | |
} | |
Vehicle.prototype.getModel = function getModel() { | |
return this.model; | |
} | |
Vehicle.prototype.getSpeed = function getSpeed() { | |
return this.speed; | |
} | |
Vehicle.prototype.getSpeedLimit = function getSpeedLimit() { | |
return this.speedLimit; | |
} | |
Vehicle.prototype.acelerate = function acelerate(speed) { | |
console.log('Acelerating to - '+speed); | |
if(speed > this.speedLimit) { | |
throw new Error('You cant acelerate more than '+this.speedLimit); | |
} | |
this.speed = speed; | |
} | |
console.log('----------------------------'); | |
console.log('\n'); | |
console.log('----TRUCK DEFINITION----'); | |
console.log('----Truck extends Vehicle----'); | |
// https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Object/create | |
function Truck(name, model, speedLimit, maximumLoad) { | |
// prevent the scope error when the programmer forgot the | |
// 'new' keyword before call the constructor. | |
if (!(this instanceof Truck)){ | |
return new Truck(name, model, speedLimit, maximumLoad); | |
} | |
this.maximumLoad = maximumLoad; | |
//Vehicle.call(this, name, model, speedLimit); | |
Vehicle.apply(this, arguments); | |
} | |
Truck.prototype = Object.create(Vehicle.prototype); | |
Truck.prototype.constructor = Truck; | |
Truck.prototype.getMaximumLoad = function getMaximumLoad() { | |
return this.maximumLoad; | |
} | |
var truck = new Truck('Fast Truck VW', 'Serie 5', 180, '2 Tons'); | |
truck.acelerate(100); | |
console.log('\n'); | |
console.log('--- Specialist functions ---') | |
console.log('Maximum Load - '+truck.getMaximumLoad()); | |
console.log('\n'); | |
console.log('--- Inherited functions ---') | |
console.log('Name - '+truck.getName()); | |
console.log('Model - '+truck.getModel()); | |
console.log('Speed - '+truck.getSpeed()); | |
console.log('Speed Limit - '+truck.getSpeedLimit()); | |
console.log('\n'); | |
console.log('--- Instance tests ---'); | |
console.log('Truck is instance of Vehicle - '+(truck instanceof Vehicle)); | |
console.log('Truck is instance of Truck - '+(truck instanceof Truck)); | |
console.log('\n'); | |
console.log('--- Has own properties tests ---'); | |
console.log('Truck has maximum load property - '+truck.hasOwnProperty('maximumLoad')); | |
console.log('Vehicle has maximum load property - '+(new Vehicle('Vehicle test', 'Test', 230)).hasOwnProperty('maximumLoad')); | |
console.log('----------------------------'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment