Last active
          January 9, 2018 17:46 
        
      - 
      
- 
        Save morrelinko/bcb36c34a8cf87fc7053849be4c8e83c to your computer and use it in GitHub Desktop. 
    JavaScript Magic Functions
  
        
  
    
      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 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 | |
| } | |
| } | 
  
    
      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