Skip to content

Instantly share code, notes, and snippets.

@wyattdanger
Created September 20, 2012 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wyattdanger/3758922 to your computer and use it in GitHub Desktop.
Save wyattdanger/3758922 to your computer and use it in GitHub Desktop.
js demo
var Plan = (function() {
// protected
var records = [];
// the Plan constructor
function Plan(id, price) {
this.id = id;
this.price = price;
records.push(this);
}
// Plan instance methods go here
Plan.prototype = {
formattedPrice: function() {
if (isNaN(this.price)) {
return this.price;
} else {
return "$" + this.price + ".00";
}
}
};
// "Class" methods
Plan.find = function(id) {
var result = records.filter(function(record) {
return record.id === id;
});
return result.length ? result[0] : null;
};
Plan.all = function() {
return records;
};
return Plan;
}());
new Plan(1,"FREE");
new Plan(2,5);
new Plan(3,10);
new Plan(4,100);
new Plan(5,1000);
console.log("All Plans: ", Plan.all());
console.log("Plan with id 4:", Plan.find(4));
console.log("Formatted price of plan with id 4:", Plan.find(4).formattedPrice());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment