Skip to content

Instantly share code, notes, and snippets.

@mishelashala
Last active November 8, 2015 07:49
Show Gist options
  • Save mishelashala/b95f0fdb6e5cf59c2e07 to your computer and use it in GitHub Desktop.
Save mishelashala/b95f0fdb6e5cf59c2e07 to your computer and use it in GitHub Desktop.
OLOO JavaScript
'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 }
'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;
'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