Typeof this in getters (was: eval on non-strings)

Domenic Denicola domenic at domenicdenicola.com
Tue Feb 28 16:20:30 PST 2012


> To clarify, the expected results here are:
> 
> Non-strict
> Typeof this inside a function when called on a number: object
> Typeof this inside an Object.prototype getter when used on a number: object
>
> Strict
> Typeof this inside a function when called on a number: number
> Typeof this inside an Object.prototype getter when used on a number: number
>
> right?

Yes.

> There’s over a hundred test cases that have already been created, and will be contributed ‘soon’ to cover https://bugs.ecmascript.org/show_bug.cgi?id=180.  One such test case is:
>  function foo()
>  {
>    'use strict';
>    return typeof(this);
>  }
>  function bar()
>  {
>    return typeof(this);
>  }
>  return foo.call(1) === 'number' && bar.call(1) === 'object';

Chrome fails this one with `bar.call(1)==='number'`, but everyone else passes.

> Another that seem relevant here is:
> function testcase() {
> var o = {};
> Object.defineProperty(o, "foo", { get: function() { "use strict"; return this; } });
> return o.foo===o;
> }

This is irrelevant because it is not testing primitives and boxing. (More formally, it is not testing the relevant part of 10.4.3 that we are discussing.)

> On that note, is there anything that’s particularly interesting spec-wise in defining foo on Object.prototype instead of a generic object and validating there? 

It's relevant because it allows use on numbers, i.e. `(0).getMe`. The same results happen if you define on `Number.prototype`.



More information about the es-discuss mailing list