Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created November 25, 2011 15:45
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 Raynos/1393837 to your computer and use it in GitHub Desktop.
Save Raynos/1393837 to your computer and use it in GitHub Desktop.
pd example
// persons modules
// uses pd - http://raynos.org/blog/17/Improving-ES5-OO-with-pd
var Person = {
fullName: function _fullName() {
return this.firstName + " " + this.lastName;
},
age: function _age() {
return (new Date).getYear() - this.birthDate.getYear();
},
saySomething: function _saySomething() {
return this.foo;
},
constructor: function _construct(hash) {
hash.foo = hash.foo || "bar";
pd.extend(this, hash);
}
};
var FitnessAddict = pd.make(Person, {
bmi: function _bmi() {
return this.weight / (this.height * this.height);
},
isWeightNormal = function _isWeightNormal() {
var bmi = this.bmi();
if (bmi > 25 || bmi < 18.5) {
return false;
}
return true;
}
});
module.exports = {
FitnessAddict: FitnessAddict
};
var FitnessAddict = require("persons").FitnessAddict;
var fa = pd.beget(FitnessAddict, {
firstName: "Bar",
lastName: "Baz",
birthYear: new Date(1990,5,5),
weight: 80,
height: 1.8
});
fa.isWeightNormal(); // true
fa.saySomething(); // "bar"
fa.fullName(); // "Bar Baz"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment