Guest User

Untitled

a guest
Feb 25th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. // call cloner with base and a list of items to clone (same type preferably) of the same type
  2. function cloner(base, clones){
  3. var handler = {
  4. get(target, name, reciever){
  5. if (typeof target[name] == "function"){
  6. // call the function with args to all contexts
  7. return function (...args){
  8. clones.forEach((x) => x[name].apply(this, args));
  9. // return whatever base returns
  10. return target[name].apply(this, args)
  11. }
  12. } else {
  13. // just return base context value if it's not a function
  14. return target[name];
  15. }
  16.  
  17. },
  18. set(obj, prop, val) {
  19. clones.forEach((x) => x[prop] = val);
  20. obj[prop] = val;
  21. return val;
  22. }
  23. }
  24. return new Proxy(base, handler);
  25. }
Add Comment
Please, Sign In to add comment