首页 > 其他 > 详细

ES6知识点整理之----Proxy----this

时间:2018-09-19 13:24:58      阅读:151      评论:0      收藏:0      [点我收藏+]

1、在 Proxy 代理的情况下,目标对象内部的this关键字会指向 Proxy 代理。

2、有些原生对象的内部属性,只有通过正确的this才能拿到,所以 Proxy 也无法代理这些原生对象的属性。

const target = new Date();
const handler = {};
const proxy = new Proxy(target, handler);

proxy.getDate();
// TypeError: this is not a Date object.

this绑定原始对象,就可以解决这个问题。

const target = new Date(‘2015-01-01‘);
const handler = {
  get(target, prop) {
    if (prop === ‘getDate‘) {
      return target.getDate.bind(target);
    }
    return Reflect.get(target, prop);
  }
};
const proxy = new Proxy(target, handler);

proxy.getDate() // 1

 

ES6知识点整理之----Proxy----this

原文:https://www.cnblogs.com/adhehe/p/9673966.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!