Skip to content

Instantly share code, notes, and snippets.

@Raynos Raynos/OO-ified.js
Created Nov 25, 2011

Embed
What would you like to do?
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
You can’t perform that action at this time.