Last active
February 27, 2017 02:39
-
-
Save pllearns/0ea15174aec40d8b604a9c8285fabe6e to your computer and use it in GitHub Desktop.
Object Oriented Javascript (from Derek Banas Tutorial)
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
Here is an efficient way to get getters and setters: | |
<!DOCTYPE HTML> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Javascript Practice</title> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
//getters and setters // | |
var Circle = function(radius) { | |
this._radius = radius; | |
} | |
Circle.prototype = { | |
set radius(radius) {this._radius = radius;}, | |
get radius() {return this._radius;}, | |
get area() {return Math.PI * (this._radius * this._radius);} | |
}; | |
var circ = new Circle(10); | |
circ.radius = 15; | |
document.write("A circle with radius " + circ.radius + " has an area of " + circ.area.toFixed(2) + "<br />") | |
</script> | |
</body> | |
</html> |
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
<!DOCTYPE HTML> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Javascript Practice</title> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
function Animal() { | |
this.name = "Animal"; | |
this.toString = function() { | |
return "My name is " + this.name; | |
}; | |
} | |
function Canine() { | |
this.name = "Canine"; | |
} | |
function Wolf() { | |
this.name = "Wolf"; | |
} | |
Canine.prototype = new Animal(); | |
Wolf.prototype = new Canine(); | |
Canine.prototype.constructor = Canine; | |
Wolf.prototype.constructor = Wolf; | |
var arcticWolf = new Wolf(); | |
document.write(arcticWolf.toString() + "<br />") | |
</script> | |
</body> | |
</html> |
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
<script type="text/javascript"> | |
function Animal() { | |
this.name = "Animal"; | |
this.toString = function() { | |
return "My name is " + this.name; | |
}; | |
} | |
function Canine() { | |
this.name = "Canine"; | |
} | |
function Wolf() { | |
this.name = "Wolf"; | |
} | |
Canine.prototype = new Animal(); | |
Wolf.prototype = new Canine(); | |
Canine.prototype.constructor = Canine; | |
Wolf.prototype.constructor = Wolf; | |
var arcticWolf = new Wolf(); | |
document.write(arcticWolf.toString() + "<br />") | |
document.write("is wolf instance of " + (arcticWolf instanceof Animal) + "<br />") | |
Animal.prototype.sound = "Grrrr" | |
Animal.prototype.getSound = function() { | |
return this.name + " says " + this.sound; | |
} | |
Canine.prototype.sound = "Woof" | |
Wolf.prototype.sound = "Grrrr Woof" | |
document.write(arcticWolf.getSound() + "<br />"); | |
function Rodent() { | |
this.name = "Rodent"; | |
} | |
function Rat() { | |
this.name = "Rat"; | |
} | |
Rodent.prototype = new Animal(); | |
Rat.prototype = Rodent.prototype; | |
Rodent.prototype.constructor = Rodent; | |
Rat.prototype.constructor = Rat; | |
var caneRat = new Rat(); | |
document.write(caneRat.toString() + "<br />"); | |
</script> |
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
function extend(Child, Parent){ | |
var Temp = function(){}; | |
Temp.prototype = Parent.prototype; | |
Child.prototype = new Temp(); | |
Child.prototype.constructor = Child; | |
} | |
function Deer() { | |
this.name = "Deer"; | |
this.sound = "Snort"; | |
} | |
extend(Deer, Animal); | |
var elk = new Deer(); | |
document.write(elk.getSound() + "<br />") |
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
function Vehicle(name) { | |
this.name = "Vehicle"; | |
} | |
Vehicle.prototype = { | |
drive: function() { | |
return this.name + " drives forward"; | |
}, | |
stop: function() { | |
return this.name + " stops"; | |
} | |
} | |
function Truck(name) { | |
this.name = name; | |
} | |
Truck.prototype = new Vehicle(); | |
Truck.prototype.constructor = Truck; | |
Truck.prototype.drive = function() { | |
var driveMsg = Vehicle.prototype.drive.apply(this); | |
return driveMsg += " through a field"; | |
} | |
var jeep = new Truck("Jeep"); | |
document.write(jeep.drive() + "<br />"); | |
document.write(jeep.stop() + "<br />"); |
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
//ECMA5 | |
var addStuff = { | |
sum: function(num1, num2) { | |
return num1 + num2; | |
} | |
}; | |
document.write("1 + 2 = ", addStuff.sum(1,2) + "<br />"); | |
//ECMA6 | |
var addStuff = { | |
sum(num1, num2){ | |
return num1 + num2; | |
} | |
} | |
document.write("1 + 2 = ", addStuff.sum(1,2) + "<br />"); |
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
//ECMA5 | |
var Point = function(xPos, yPos){ | |
this.xPos = xPos; | |
this.yPos = yPos; | |
}; | |
Point.prototype.getPos = function(){ | |
return "X: " + this.xPos + " Y : " + this.yPos; | |
}; | |
var point = new Point(100, 200); | |
document.write("Point Pos : " + point.getPos() + "<br />"); | |
//ECMA6 | |
class Point { | |
constructor(xPos, yPos) { | |
this.xPos = xPos; | |
this.yPos = yPos; | |
} | |
getPos(){ | |
return "X: " + this.xPos + " Y : " + this.yPos; | |
} | |
} | |
var point = new Point(100, 200); | |
document.write("Point Pos : " + point.getPos() + "<br />"); |
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
class Animal { | |
constructor(name) { | |
this.name = name; | |
} | |
toString() { | |
return "Animal is name " + this.name; | |
} | |
static getAnimal() { | |
return new Animal("No Name") | |
} | |
} | |
class Dog extends Animal { | |
constructor(name, owner) { | |
super(name); | |
this.owner = owner; | |
} | |
toString() { | |
return super.toString() + "<br />Dog is named " + this.name; | |
} | |
} | |
var rover = new Dog("Rover", "Paul"); | |
document.write(rover.name + " is owned by " + rover.owner + "<br />"); | |
document.write(rover.toString() + "<br />"); | |
var bowser = Animal.getAnimal(); | |
document.write(" Bowser info : " + bowser.toString() + "<br />"); |
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
<!DOCTYPE HTML> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Javascript Practice</title> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
function Hero(name) { | |
if(typeof Hero.instance === 'object') { | |
return Hero.instance; | |
} | |
this.name = name; | |
Hero.instance = this; | |
return this; | |
} | |
var phillipHero = Hero("Phil"); | |
document.write("Our hero is " + phillipHero.name + "<br />"); | |
var sallyHero = Hero("Sally"); | |
document.write("Our hero is " + sallyHero.name + "<br />"); | |
</script> | |
</body> | |
</html> |
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
function Sword(desc){ | |
this.weaponType = "Sword"; | |
this.metal = desc.metal || "Steel"; | |
this.style = desc.style || "Longsword"; | |
this.hasMagic = desc.hasMagic || false; | |
} | |
function Bow(desc){ | |
this.weaponType = "Bow"; | |
this.material = desc.metal || "Wood"; | |
this.style = desc.style || "Longbow"; | |
this.hasMagic = desc.hasMagic || false; | |
} | |
function WeaponFactory(){}; | |
WeaponFactory.prototype.makeWeapon = function(desc) { | |
var weaponClass = null; | |
if(desc.weaponType === "Sword"){ | |
weaponClass = Sword; | |
} else if (desc.weaponType === "Bow"){ | |
weaponClass = Bow; | |
} else { | |
return false; | |
} | |
return new weaponClass(desc); | |
} | |
var myWeaponFactory = new WeaponFactory(); | |
var bladeFist = myWeaponFactory.makeWeapon({ | |
weaponType: "Sword", | |
metal: "Dark Iron", | |
style: "Scythe", | |
hasMagic: true | |
}); | |
document.write(bladeFist.weaponType + " of type " + bladeFist.style + " crafted from " + bladeFist.metal + "<br />"); |
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
function Pizza(price) { | |
this.price = price || 10; | |
} | |
Pizza.prototype.getPrice = function() { | |
return this.price | |
} | |
function ExtraCheese(pizza) { | |
var prevPrice = pizza.price; | |
pizza.price = prevPrice + 1; | |
} | |
var myPizza = new Pizza(10); | |
ExtraCheese(myPizza); | |
document.write("Cost of Pizza : $" + myPizza.price + "<br />"); |
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
var Observable = function() { | |
this.subscribers = []; | |
} | |
Observable.prototype = { | |
subscribe: function(subscriber){ | |
this.subscribers.push(subscriber); | |
}, | |
unsubscribe: function(unsubscriber) { | |
for(i = 0; i < this.subscribers.length; i++) { | |
if(this.subscribers[i] === unsubscriber) { | |
this.subscribers.splice(i, 1); | |
return unsubscriber.name; | |
} | |
} | |
}, | |
publish: function(data) { | |
for(i = 0; i < this.subscribers.length; i++) { | |
this.subscribers[i].receiveData(data); | |
} | |
} | |
}; | |
var MorganFanny = { | |
name: "Morgan Fanny", | |
receiveData: function(data) { | |
document.write(this.name + " received your info " + data + "<br />"); | |
} | |
} | |
var BoldmanYaks = { | |
name: "Boldman Yaks", | |
receiveData: function(data) { | |
document.write(this.name + " received your info " + data + "<br />"); | |
} | |
} | |
observable = new Observable(); | |
observable.subscribe(MorganFanny); | |
observable.subscribe(BoldmanYaks); | |
observable.publish('IBM at $145.30'); | |
document.write(observable.unsubscribe(MorganFanny) + " unsubscribed<br />"); | |
observable.publish('IBM at $144.30'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment