ES5 riddle -- this binding in accessors accessed as global vars

# Mark S. Miller (14 years ago)

According to the ES5 spec, should the following program alert the global object or undefined? Why?

Object.defineProperty(this, 'foo', {get:function(){"use strict"; return

this;}}); alert((function(){"use strict"; return foo;})());

# Allen Wirfs-Brock (14 years ago)

According to the spec. it should alert the global object: The reference to foo resolves to a Reference to an object environment record. When getValue is called on that reference it goes through step 5.a and calls GetBindingValue on the env record. GetBindingValue does a [[Get]] on the global object. [[Get]] internally retrieves an accessor property and does a call to the [[Get]] function passing the global objet as the this value.

# Mark S. Miller (14 years ago)

On Mon, May 2, 2011 at 5:50 PM, Allen Wirfs-Brock <allen at wirfs-brock.com>wrote:

According to the spec. it should alert the global object: The reference to foo resolves to a Reference to an object environment record. When getValue is called on that reference it goes through step 5.a and calls GetBindingValue on the env record. GetBindingValue does a [[Get]] on the global object. [[Get]] internally retrieves an accessor property and does a call to the [[Get]] function passing the global objet as the this value.

It's not the answer I was hoping for, but on reexamination I see that you're correct. Thanks.