Skip to content

Instantly share code, notes, and snippets.

@ehsanullahjan
Last active August 29, 2015 14:21
Show Gist options
  • Save ehsanullahjan/601b1046de76651d4e8a to your computer and use it in GitHub Desktop.
Save ehsanullahjan/601b1046de76651d4e8a to your computer and use it in GitHub Desktop.
"use strict";
// Base Girl object.
function Girl() {
this.name = '';
this.traits = {};
}
// Scout `is a` Girl
function Scout() {
Girl.call(this);
this.team = '';
}
Scout.prototype = new Girl();
Scout.prototype.constructor = Scout;
// Setup scout: Sarah
var sarah = new Scout();
sarah.name = "Sarah";
sarah.team = "Blue Scouts";
sarah.traits.age = 30;
sarah.traits.weight = 125;
// Setup scout: Tricia
var tricia = new Scout();
tricia.name = "Tricia";
tricia.team = "Green Scouts";
tricia.traits.age = 32;
tricia.traits.weight = 140;
// Log the girls
console.log("Sarah:", sarah, "\n");
console.log("Tricia:", tricia, "\n");
// Sarah (and Tricia) are both girl scouts
console.log(sarah instanceof Scout);
console.log(sarah instanceof Girl);
@ehsanullahjan
Copy link
Author

A simple and clean example of this pattern can be seen here: http://bit.ly/1PESNli

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment