Created
January 11, 2018 19:53
-
-
Save jsmayo/8ea2cf2ca5e0239b23a0e3327cc3f71c to your computer and use it in GitHub Desktop.
Objects: OO -> OLOO created by jsmayo - https://repl.it/@jsmayo/Objects-OO-greater-OLOO
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
/* | |
ORIGINAL OO CODE: | |
var Vehicle = function (x, y, speed) { | |
this.x = x; | |
this.y = y; | |
this.speed = speed; | |
}; | |
Vehicle.prototype.move = function() { | |
this.x = this.x + this.speed * this.randomStep(); | |
this.y = this.y + this.speed * this.randomStep(); | |
} | |
Vehicle.prototype.randomStep = function() { | |
if (Math.random() < 0.5) { | |
return -1; | |
} else { | |
return 1; | |
} | |
} | |
var Car = function (x, y) { | |
Vehicle.call(this, x, y, 5); | |
}; | |
Car.prototype = Object.create(Vehicle.prototype); | |
Car.prototype.honk = function () { | |
console.log("Beep! Beep!"); | |
}; | |
var Bicycle = function (x, y) { | |
Vehicle.call(this, x, y, 2); | |
}; | |
Bicycle.prototype = Object.create(Vehicle.prototype); | |
Bicycle.prototype.honk = function () { | |
// this.constructor.honk.call(this); | |
console.log("Hey! Watch out!"); | |
}; | |
var AngryBicycle = function (x, y) { | |
Bicycle.call(this, x, y); | |
}; | |
AngryBicycle.prototype = Object.create(Bicycle.prototype); | |
AngryBicycle.prototype.honk = function () { | |
Object.getPrototypeOf(AngryBicycle.prototype).honk.call(this); | |
Object.getPrototypeOf(AngryBicycle.prototype).honk.call(this); | |
}; | |
*/ | |
var Vehicle = { | |
move: function(x, y, speed) { | |
this.x = this.x + this.speed * this.randomStem(); | |
this.y = this.y + this.speed * this.randomStep(); | |
}, | |
randomStep: function() { | |
if (Math.random < 0.5) return -1; | |
else return 1; | |
}, | |
}; | |
var Car = Object.create(Vehicle); | |
Car.prepareCar = function(x, y, speed = 5) { | |
this.move(x, y, speed); | |
}; | |
Car.honk = function() { | |
console.log('Beep!'); | |
}; | |
var Bicycle = Object.create(Vehicle); | |
Bicycle.honk = function() { | |
console.log('Hey! Watch it!!'); | |
}; | |
var AngryBicycle = Object.create(Bicycle); | |
AngryBicycle.honk = function() { | |
console.log('GRRR SO ANGRY, MOOOOOOVE!!!!'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment