Created
September 20, 2012 23:21
-
-
Save wyattdanger/3758922 to your computer and use it in GitHub Desktop.
js demo
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
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