0

When I run the following code:

var obj = {get foo(){return false}};
Object.create(obj);

in the Chrome DevTools console, I get the following:

{
    foo: (...)
}

where I have to specifically click to invoke the getter, rather than the invocation happening automatically.

However when I run the following code:

var obj = new CustomEvent('test_event', {detail: false});
obj

I see the following result:

CustomEvent{
   bubbles: false
   cancelBubble: false
   cancelable: false
   composed: false
   currentTarget: null
   defaultPrevented: false
   detail: false
   eventPhase: 0
   isTrusted: false
   path: []
   returnValue: true
   srcElement: null
   target: null
   timeStamp: 3209573.314999696
   type: "test_event"
}

On closer inspection, it is discovered that detail is a getter on the CustomEvent prototype, but it is invoked immediately i.e. no (...).

In this issue on the Firefox DevTools Github page [that I stumbled when doing research on how Chrome handles getter invocation], a respondent says:

Chrome does not invoke unsafe getters by default, but you can click (...) to do so

What is meant by a getter being unsafe? Why is this [CustomEvent] getter invoked, and the earlier example not? Why is it considered safe?

ObiHill
  • 11,448
  • 20
  • 86
  • 135
  • 1
    Does this answer your question? [Precisely what are the general rules for which expressions results Chrome DevTools' "Eager evaluation" preview text will show?](https://stackoverflow.com/questions/55368539/precisely-what-are-the-general-rules-for-which-expressions-results-chrome-devtoo) – Justinas Mar 01 '21 at 08:26
  • @Justinas Not quite. It doesn't explain the discrepancy to my full understanding. Why would returning a primitive in the getter e.g. `get foo(){return false}` or `get foo(){return 1}` be considered unsafe? I'd appreciate the knowledge on the rules. – ObiHill Mar 01 '21 at 09:26

0 Answers0