Created
January 9, 2018 17:47
-
-
Save morrelinko/9ad3078475d73c5899d9e3521d1bed9a to your computer and use it in GitHub Desktop.
JS Magic Methods
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
'use strict' | |
const assert = require('assert') | |
module.exports = function (obj) { | |
return new Proxy(obj, { | |
get (target, prop, receiver) { | |
if (prop in target) { | |
return Reflect.get(target, prop, receiver) | |
} | |
if (target.__get) { | |
return target.__get(prop) | |
} | |
}, | |
set (target, prop, value, receiver) { | |
if (prop in target) { | |
return Reflect.set(target, prop, value, receiver) | |
} | |
if (target.hasOwnProperty('__set')) { | |
target.__set(prop) | |
} | |
return true | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example