0

I would like to be able to observe when a global object gets created.

I was hoping to do something with JavaScript Proxy.

let objectA = undefined;

...  Proxy code here

A little while later...

objectA = { prop1: 'hello', prop2: 'world' }

... proxy code gets called here... console.log(objectA)
2
  • 1
    Not if you're creating your object as a literal like that, no. If you have a class or a function with a constructor, you can use the construct trap. Commented Jan 31, 2019 at 20:54
  • Exact duplicate of stackoverflow.com/questions/38759116/… Commented Jan 31, 2019 at 21:29

1 Answer 1

1

I would like to be able to observe when a global object gets created. I was hoping to do something with JavaScript Proxy.

No, Proxy doesn't do that. A proxy is an already existing object, and you cannot wrap the global object (window) in a proxy either.

You could however install a setter on window.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.