Last active
August 29, 2015 14:00
-
-
Save ultim8k/11057159 to your computer and use it in GitHub Desktop.
Object oriented Javascript
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
// Object Constructor | |
var Suit = function(name){ | |
// Private value | |
var name = name; | |
// Private function | |
function getName(){ | |
return name; | |
} | |
// Public function | |
this.sayHi = function(){ | |
var n = getName(); | |
console.log('Hi ' + n + '!'); | |
} | |
} | |
// Create new instances | |
var first_suit = new Suit('Mark One'); | |
var second_suit = new Suit('Mark 2'); | |
// Call the public functions for each instance | |
first_suit.sayHi() | |
second_suit.sayHi() | |
// Alter the initial object constructor | |
Suit.prototype.getPowerSource = function(){ | |
console.log('My power source is Palladium'); | |
// Vibranium | |
} | |
// Our instances get updated automatically | |
first_suit.getPowerSource() | |
second_suit.getPowerSource() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment