Last active
November 8, 2015 07:49
-
-
Save mishelashala/b95f0fdb6e5cf59c2e07 to your computer and use it in GitHub Desktop.
OLOO JavaScript
This file contains 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
'use strict'; | |
// Worker as a module, not as an object. | |
// Encapsulates behavior. | |
const Worker = require('./worker'); | |
// Factory method instead of constructor | |
const jd = Worker.create('Jenny Doe', 19, 'CEO'); | |
// Own properties | |
console.log(jd); // { name: 'Jenny Doe', age: 19, job: 'CEO' } | |
// Prototypes' chain! | |
console.log(jd.__proto__); // { job: 'Offinist' } | |
console.log(jd.__proto__.__proto__); // { name: 'John Doe', age: 20 } |
This file contains 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
'use strict'; | |
// Base Object, default values. | |
const Person = { | |
name: 'John Doe', | |
age: 20 | |
}; | |
// Factory function | |
const createPerson = function (name, age) { | |
const proto = Object.create(Person); | |
const props = { name, age }; | |
return Object.assign(proto, props); | |
} | |
// Exposing the module | |
exports.proto = Person; | |
exports.create = createPerson; |
This file contains 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
'use strict'; | |
// Person as a module, not as an object. | |
// Encapsulates behavior. | |
const Person = require('./person'); | |
// Parent: Person, default values. | |
const Worker = { | |
__proto__: Person.proto, | |
job: 'Officinist' | |
}; | |
// Factory function | |
const createWorker = function (name, age, job) { | |
const parent = Person.create(name, age); | |
const proto = Object.create(worker); | |
const props = { __proto__: parent, job }; | |
return Object.assign(proto, props); | |
} | |
// Exposing the module | |
exports.proto = Worker; | |
exports.create = createWorker; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment