Created
November 25, 2011 15:45
-
-
Save Raynos/1393837 to your computer and use it in GitHub Desktop.
pd example
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
// 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 | |
}; |
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 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