Skip to content

Instantly share code, notes, and snippets.

@morrelinko
Last active January 9, 2018 17:46
Show Gist options
  • Save morrelinko/bcb36c34a8cf87fc7053849be4c8e83c to your computer and use it in GitHub Desktop.
Save morrelinko/bcb36c34a8cf87fc7053849be4c8e83c to your computer and use it in GitHub Desktop.
JavaScript Magic Functions
'use strict'
const magic = require('./magic')
class Request {
constructor(req) {
this.req = req
this.user = 'morrelinko'
}
__get (prop) {
return this.req[prop]
}
__set (prop, value) {
this.req[prop] = value
}
}
'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