Skip to content

Instantly share code, notes, and snippets.

@gabeno
Created April 15, 2014 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabeno/10740659 to your computer and use it in GitHub Desktop.
Save gabeno/10740659 to your computer and use it in GitHub Desktop.
overview on classes and objects in JS
// the original Animal class and sayName method
function Animal(name, numLegs) {
this.name = name;
this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
console.log("Hi my name is " + this.name);
};
// define a Penguin class
function Penguin(name) {
this.name = name; // unique for each penguin
this.numLegs = 2; // all penguins have 2 legs, so we set this
}
// set its prototype to be a new instance of Animal
Penguin.prototype = new Animal(); // inherits from Animal class
var penguin = new Penguin('pingu');
penguin.sayName(); // => Hi my name is Pingu
// check what properties and methods the child class has
// prototype chain
// private properties
function Person(first,last,age) {
this.firstname = first;
this.lastname = last;
this.age = age;
var bankBalance = 7500; // private variable
this.getBalance = function() { // access the private variable
return bankBalance;
};
}
var john = new Person('John', 'Bravo', 33);
// try to print his bankBalance
console.log(john.bankBalance); // => undefined
var myBalance = john.getBalance();
console.log(myBalance); // => 7500
// private methods
function Person(first,last,age) {
this.firstname = first;
this.lastname = last;
this.age = age;
var bankBalance = 7500;
var returnBalance = function() {
return bankBalance;
};
this.askTeller = function() {
return returnBalance;
}
}
var john = new Person('John','Smith',30);
console.log(john.returnBalance); // => undefined
var myBalanceMethod = john.askTeller(); // => returnBalance
var myBalance = myBalanceMethod();
console.log(myBalance); // => 7500
// sample cash register app
function StaffMember(name,discountPercent){
this.name = name;
this.discountPercent = discountPercent;
}
var sally = new StaffMember("Sally",5);
var bob = new StaffMember("Bob",10);
// Create yourself again as 'me' with a staff discount of 20%
var me = new StaffMember('Gab', 20);
var cashRegister = {
total:0,
lastTransactionAmount: 0,
add: function(itemCost){
this.total += (itemCost || 0);
this.lastTransactionAmount = itemCost;
},
scan: function(item,quantity){
switch (item){
case "eggs": this.add(0.98 * quantity); break;
case "milk": this.add(1.23 * quantity); break;
case "magazine": this.add(4.99 * quantity); break;
case "chocolate": this.add(0.45 * quantity); break;
}
return true;
},
voidLastTransaction : function(){
this.total -= this.lastTransactionAmount;
this.lastTransactionAmount = 0;
},
// Create a new method applyStaffDiscount here
applyStaffDiscount: function(employee) {
return this.total -= this.total * (employee.discountPercent / 100);
}
};
cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',3);
// Apply your staff discount by passing the 'me' object
// to applyStaffDiscount
cashRegister.applyStaffDiscount(me);
// Show the total bill
console.log('Your bill is '+cashRegister.total.toFixed(2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment