Created
          February 26, 2020 08:50 
        
      - 
      
- 
        Save bearzk/e6128b2e242c2c68c3f2f71cb4b6821d to your computer and use it in GitHub Desktop. 
    [proxy] #js
  
        
  
    
      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
    
  
  
    
  | /* eslint-disable max-classes-per-file */ | |
| class Repo { | |
| constructor() { | |
| this.name = 'maomao'; | |
| } | |
| work(p1, p2) { | |
| return (p1 + p2); | |
| } | |
| hey(name) { | |
| return name; | |
| } | |
| } | |
| // if method is not defined on paginato, | |
| // we want to try call on the this.repo with same params | |
| class Paginator { | |
| constructor(repo) { | |
| this.repo = repo; | |
| this.name = 'yoyo'; | |
| } | |
| work(p1, p2) { | |
| return (p1 * p2); | |
| } | |
| } | |
| const repo = new Repo(); | |
| const pa = new Paginator(repo); | |
| const paginator = new Proxy(pa, { | |
| get: function get(target, name) { | |
| if (Object.prototype.hasOwnProperty.call(target, name)) { | |
| return target[name]; | |
| } | |
| return function wrapper() { | |
| const args = Array.prototype.slice.call(arguments); | |
| if (typeof target[name] === 'function') { | |
| return target[name](...args); | |
| } | |
| return target.repo[name](...args); | |
| }; | |
| }, | |
| }); | |
| console.log(paginator.work(9, 2)); | |
| console.log(paginator.hey('you')); | |
| console.log(paginator.name); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment