Simple question I am trying the following in my console

let a = new Proxy(new Date(), {})

I am expecting to be able to call

a.getMonth();

but it does not work it throws:

Uncaught TypeError: this is not a Date object. at Proxy.getMonth (<anonymous>)
at <anonymous>:1:3

Funny part is that in Chrome the autocomplete does suggest all the Date functions on a. What am I missing?

Edit in response for @Bergi

I realized that there is a bug in this code aside for my question but here is what I am trying to do:

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);
}

Now getData() returns some dates

  • Proxies don't work well on builtin objects. Why do you try to use one on dates? – Bergi Dec 18 '17 at 18:47
  • @Bergi I am implementing this and my date fields were not working. – MotKohn Dec 18 '17 at 18:49
  • You'd need your proxy to return a wrapper on getMonth that would call it with the original target rather than the proxy. – loganfsmyth Dec 18 '17 at 18:57
  • @Bergi specifically I'm trying to have some simple change tracking and it all works except for dates – MotKohn Dec 18 '17 at 18:57
  • What do you mean by "implementing this" (the script you linked only does some property access logging)? What is your actual problem? How do you want it to work with dates? (Preventing dates from changing or detecting mutations of dates is extra hard) – Bergi Dec 18 '17 at 18:59
up vote -1 down vote accepted

My original answer was all wrong. But the following handler should work

    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());

  • Not sure why the down voting, it works on the fiddle and it follows the documentation. – richbai90 Dec 18 '17 at 18:56
  • 1
    @richbai90 One does not need to use a separate variable for the target. And the whole question is how to make p.getMonth() working - "use target.getMonth() instead" does not answer that – Bergi Dec 18 '17 at 19:01
  • 1
    It might have been better to delete this answer and post a new one, to avoid starting with downvotes. Btw, this still doesn't completely work, as you only must bind functions not any property that exists in the target. – Bergi Dec 18 '17 at 21:16
  • It works but just as @Bergi pointed out additional guards have to be in place in my case. e.g. make sure target is a date, make sure name is a function. This has nothing to do with your answer but in the end I can't use this because angular.isDate() does not recognize Proxyies as a date. I am trying to get them to reopen a closed issue. Also it would also be nice if you can explain why I would need to do this. – MotKohn Dec 18 '17 at 22:17
  • 1
    @Bergi you're right I didn't realize that (without ridiculous work, like trapping all the set* funtions) I won't observe mutations. Thanks – MotKohn Dec 19 '17 at 14:21

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.