Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does console.log not trigger proxy getter trap?

Tags:

javascript

I have following code:

let p = new Proxy([1, 2, 3], {   
  get: function() {   
    console.log('get')   
  }   
})
console.log(p)

I think Proxy should proxy everything about [1,2,3].

When I log value, it should read value from [1,2,3], so the getter should be triggered.

But when I set a breakpoint in the getter, the breakpoint is not hit?

Why don't console.log and console.table trigger the getter function?

like image 277
nuclear Avatar asked Apr 11 '19 10:04

nuclear


People also ask

How do I call the get trap from a proxy?

Calling console.log ( util.inspect with default opts) on an instance of a Proxy invokes the get trap with some built-in Symbols. (It also invokes the trap with "inspect" and "valueOf", which is different than browser behavior but doesn't seem as problematic.)

Why is the FCC ‘console log’ not working?

Make sure you are using the browser’s console (Ctrl + Shft+ J in Chrome) and not the fake console that FCC provides. That absolutely works. Weird how the FCC ‘console log’ just quit working. It was working fine for months and stopped about an hour ago.

How to get console output when console is not active?

Most of the time calling console.log () when the console is not yet active only results in a reference to the object being queued, not the output the console will contain. As a workaround you will need to either clone the information or serialize snapshots of it.


1 Answers

That's because you are logging the Proxy object itself.
The browser console has access to its internal state, which is an Object with two or three internal properties: [[target]], [[handler]] and [[IsRevoked]].

You may note that Chrome does by default expands the [[target]] in its collapsed message:

console output of Chrome

while Firefox choose to expose only the Proxy object by default (and doesn't expose an [[isRevoked]] internal, they set <target> and <handler> to null to signal revoked Proxies).
enter image description here

So as to how they are able to not trigger the trap, well they take the internal [[target]] shortcut and access directly to the original Object.

like image 53
Kaiido Avatar answered Sep 22 '22 18:09

Kaiido