Skip to content

Instantly share code, notes, and snippets.

@Gergling
Created April 12, 2018 09:46
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 Gergling/9a754c0f847a18aaeaca7b1bf78af30d to your computer and use it in GitHub Desktop.
Save Gergling/9a754c0f847a18aaeaca7b1bf78af30d to your computer and use it in GitHub Desktop.
A mutable object for functional programming.
// Deliberately not using class because I want data to be private.
function MutableObject() {
const data = {};
const get = name => (name === undefined) ? data : data[name];
const setObject = value => Object.assign(data, value);
const setByFunction = fnc => fnc(this);
const setNameValue = (name, value) => data[name] = value;
const set = (a, b) => {
if (typeof a === 'object') {
setObject(a);
} else if (typeof a === 'function') {
setByFunction(a);
} else {
setNameValue(a, b);
}
return this;
}
this.get = get;
this.set = set;
}
module.exports = MutableObject;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment