Skip to content

Instantly share code, notes, and snippets.

@ultim8k
Last active August 29, 2015 14:00
Show Gist options
  • Save ultim8k/11057159 to your computer and use it in GitHub Desktop.
Save ultim8k/11057159 to your computer and use it in GitHub Desktop.
Object oriented Javascript
// 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