Created
April 12, 2018 09:46
-
-
Save Gergling/9a754c0f847a18aaeaca7b1bf78af30d to your computer and use it in GitHub Desktop.
A mutable object for functional programming.
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
// 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