日期对象的代理

Mot*_*ohn 5 javascript ecmascript-6

一个简单的问题,我正在控制台中尝试以下操作

let a = new Proxy(new Date(), {})
Run Code Online (Sandbox Code Playgroud)

我希望能够打电话

a.getMonth();
Run Code Online (Sandbox Code Playgroud)

但它不起作用,它抛出:

未捕获的TypeError:这不是Date对象。在Proxy.getMonth(<匿名>)
在<匿名>:1:3

有趣的是,Chrome浏览器中的自动完成Date功能会建议所有功能a。我想念什么?

以回应方式编辑 @Bergi

我意识到这段代码中除了我的问题之外还有一个错误,但这是我想做的事情:

class myService {
...

makeProxy(data) {
    let that = this;
    return new Proxy (data, {
        cache: {},
        original: {},
        get: function(target, name) {
            let res = Reflect.get(target, name);
            if (!this.original[name]) {
                this.original[name] = res;
            }

            if (res instanceof Object && !(res instanceof Function) && target.hasOwnProperty(name)) {
                res = this.cache[name] || (this.cache[name] = that.makeProxy(res));
            }
            return res;
        },
        set: function(target, name, value) {
            var res = Reflect.set(target, name, value);

            that.isDirty = false;
            for (var item of Object.keys(this.original))
                if (this.original[item] !== target[item]) {
                    that.isDirty = true;
                    break;
                }

            return res;
        }
    });
}

getData() {
    let request = {
     ... 
    }
    return this._$http(request).then(res => makeProxy(res.data);
}
Run Code Online (Sandbox Code Playgroud)

现在getData()返回一些日期

ric*_*i90 2

我原来的答案全错了。但以下处理程序应该可以工作

    const handler = {
        get: function(target, name) {
            return name in target ?
                target[name].bind(target) : undefined
        }
    };


    const p = new Proxy(new Date(), handler);
    
    console.log(p.getMonth());
Run Code Online (Sandbox Code Playgroud)