0

Run this code in the console in Safari (15.1) and another browser (eg. Brave/Chrome):

const a = {b: {c: 5}}
Object.defineProperty(a.b, 'self', {get () {return this}})
console.log(a.b.self) // expected: {c: 5}
console.log(a) // when expanding a.b.self in inspector, expects the same, ie. {c: 5}

Then, expand the last object, until you get self: (...), now, expanding it should be equivalent to running a.b.self? It is in Brave, but I get the a object in Safari.

Hypothesis: Safari devtools has a bug where it uses the object logged, and not the prop parent, as this.

Or is this UB and I've gotten something wrong?

safari result brave results

Leonard Pauli
  • 2,662
  • 1
  • 23
  • 23
  • `this` changes depending on how the method is called. https://stackoverflow.com/questions/1197822/how-does-this-work-in-javascript -- since a getter is being used, I'm not sure what `this` should be – evolutionxbox Dec 13 '21 at 12:58
  • 1
    https://stackoverflow.com/questions/36956688/how-to-change-scope-of-javascript-getter seems to suggest that `this` is not reliable in a getter. – evolutionxbox Dec 13 '21 at 13:01
  • 1
    @evolutionxbox in this case the new property is being defined on `a.b`, so `this` should refer to that object and it's not weird. – Pointy Dec 13 '21 at 13:05
  • @Pointy that's good to know – evolutionxbox Dec 13 '21 at 13:37
  • 1
    OP, don't be fooled by console behavior. There is no standard, and each browser console has its own ideas of how it can make your life better. They are not consistent. – Pointy Dec 13 '21 at 13:40

0 Answers0