Search code examples
javascriptecmascript-5

1 presumably not equal to 1


I got a surprise today and I couldn't find the appropriate part of the specification to find out whether this was to be expected or not. My money is on that there's nothing wrong with the universe but what are the rules that make this expression evaluate to false

(function(){ return this;}).call(1) === 1

Solution

  • what are the rules that make this expression evaluate to false

    In "loose" mode the value of this is always coerced to an object. Values of type Object are not (strict) equal to values of type Number (or any other type for that matter):

    new Number(1) === 1 // false
    

    From the spec:

    Entering Function Code

    1. If the function code is strict code, set the ThisBinding to thisArg.
    2. Else if thisArg is null or undefined, set the ThisBinding to the global object.
    3. Else if Type(thisArg) is not Object, set the ThisBinding to ToObject(thisArg).
      ...