2

My target object is Notification.prototype and I need to proxy the Object.getOwnPropertyDescriptor() calls to it. I'm unable decide where to assign my proxy into.

var myProxy = new Proxy(obj, {
    get: function(target, key) {
      console.log("GET CALLED");
      return target[key];
    },
    ownKeys: function() {
      console.log("Own Keys called");
      return ["a", "b"];
    },
    getOwnPropertyDescriptor: function(target, key) {
       console.log("Property Descriptor Called");
       return { value: this.get(target, key), enumerable: true, configurable: true };
    }
});

Do I need to proxy this Object.prototype.getOwnPropertyDescriptor itself to achieve this effect?

jeffbRTC
  • 1,701
  • 3
  • 16
  • The `getOwnPropertyDescriptor` trap looks to work just fine, [keeping in mind the restrictions](https://tc39.es/ecma262/#sec-object.getownpropertydescriptor) with a Proxy – CertainPerformance Feb 04 '21 at 16:35
  • @CertainPerformance Yes but I don't know where to assign my trap. – jeffbRTC Feb 04 '21 at 16:39
  • The `getOwnPropertyDescriptor` trap in your code works. I don't understand what the problem is. – CertainPerformance Feb 04 '21 at 16:43
  • It's not possibly to replace native objects by proxied versions of themselves. How exactly are you using notifications and what do you want to intercept on them? – Bergi Feb 04 '21 at 16:44
  • @CertainPerformance The problem is I need to override Object.getOwnPropertyDescriptor for Notification.prototype object universally and I can't assign this proxy to Notification.prototype directly – jeffbRTC Feb 04 '21 at 16:44
  • Does something like this works? `Notification.prototype = new Proxy(Notification.prototype, { ... })` – adiga Feb 04 '21 at 16:45
  • 1
    @adiga No, it doesn't. Native instances are created from the original prototype directly, not looking up `.prototype` like a constructor call. – Bergi Feb 04 '21 at 16:46
  • @Bergi I'm trying to hide a property on a native object and I need to override the behavior of Object.getOwnPropertyDescriptor (and others) ... I don't mean to replace native objects – jeffbRTC Feb 04 '21 at 16:46
  • @jeffbRTC Which property, on which specific native object? As I said, you cannot replace (or "modify") `Notification.prototype`. Instead you need to intercept the creation of the concrete instance to mess with it. – Bergi Feb 04 '21 at 16:47
  • @Bergi this is a native prototype? This one: https://developer.mozilla.org/en-US/docs/Web/API/notification ? – adiga Feb 04 '21 at 16:47
  • @Bergi Notification.prototype itself. I have a custom property defined on it and I need to hide it. – jeffbRTC Feb 04 '21 at 16:49
  • @adiga Yes, it's a builtin prototype – Bergi Feb 04 '21 at 16:49
  • @jeffbRTC Hide it from where/whom? Why did you define it in the first place? – Bergi Feb 04 '21 at 16:50
  • @Bergi From websites that loop over the prototype. I'm doing some abuse blocking script. I have to define it because to maintain state. – jeffbRTC Feb 04 '21 at 16:51
  • @jeffbRTC If you want to count notifications or store some other stuff, use a symbol-keyed property, or a `WeakMap`/`WeakSet`. Do not mess with the prototype in environments you don't own. – Bergi Feb 04 '21 at 17:20
  • @Bergi Can you add an example for both two? – jeffbRTC Feb 04 '21 at 17:22
  • @Bergi Env is mine, my browser. – jeffbRTC Feb 04 '21 at 17:22
  • @jeffbRTC https://stackoverflow.com/a/33533611/1048572 – Bergi Feb 04 '21 at 17:47
  • @Bergi I like Weekmaps but does it actually sync the object or just keep copy of a some old state? – jeffbRTC Feb 04 '21 at 17:56
  • @jeffbRTC What do you mean by "sync"? You still haven't shown the code of what you want to use with `Notification.prototype` – Bergi Feb 04 '21 at 18:01
  • @Bergi Well, does it sync the object signature to the map? I mean can it differ objects? – jeffbRTC Feb 04 '21 at 19:12
  • @jeffbRTC Objects are reference values that are compared by identity, not their shape or property values. In maps and otherwise. – Bergi Feb 04 '21 at 19:22
  • 1
    @Bergi Bravo! It's working. Lovely. You may write an answer and I will accept it. – jeffbRTC Feb 04 '21 at 19:54
  • I can't put that as an answer to "*How to trap getOwnPropertyDescriptor on native object's prototype object?*" though – Bergi Feb 04 '21 at 20:11

0 Answers0