archives

« Bugzilla Issues Index

#4315 — Extend Object.is to reject suspicious invocation patterns


The ES6 specification defines `Object.is` as follows:

> 19.1.2.10 Object.is ( value1, value2 )
>
> When the is function is called with arguments value1 and value2 the following
> steps are taken:
>
> 1. Return SameValue(value1, value2).

This allows for two somewhat bizarre invocations:

Object.is(undefined); // true
Object.is(); // true

The latter looks more strange, but it is unlikely to cause problems in real code. The former presents a refactoring hazard. Generally speaking, code written like this:

if (Object.is(x,y[z])) {
// etc.
}

May be mistakenly refactored as:

if (Object.is(x[z])) {
// etc.
}

This kind of problem is most frequently exhibited by JavaScript assertion libraries today. By silently accepting suspicious invocation patterns, functions like these miss an important opportunity to alert developers of probable sources of errors.

I'd like to propose an extension to the implementation of `Object.is`:

> 19.1.2.10 Object.is ( value1, value2 )
>
> When the is function is called with arguments value1 and value2 the following
> steps are taken:
>
> 1. If value1 is not present or if value2 is not present,
> then throw a TypeError exception.
> 2. Return SameValue(value1, value2).