Andzej Maciusovic
Andzej Maciusovic

Reputation: 4476

Object.keys behavior in chrome and IE 11

Today I got error using Object.keys because accidentally I passed non object value like this:

var filter = true;
var filterKeys = Object.keys(filter);

In Chrome this works well, but in IE 11 I got exception and after debugging found that in IE 11 Object.keys was throwing exception Object.keys: argument is not an Object.

In this situation IE11 behaved better because value true is really invalid, but chrome returned empty array. Object.keys is ECMAScript standart and if you look at http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.14 it says:

  1. If the Type(O) is not Object, throw a TypeError exception.

So does anyone know why Google Chrome implementation behaves not like in ECMAScript specification standard, because I always thought that all modern browsers should implement ECMAScript to behave the same way.

Upvotes: 7

Views: 9214

Answers (2)

Jaromanda X
Jaromanda X

Reputation: 1

n this situation IE11 behaved better because value true is really invalid, but chrome returned empty array. Object.keys is ECMAScript standart and if you look at http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.14 it says:

If the Type(O) is not Object, throw a TypeError exception.

ES5 is old

read the ES6 spec and you'll see Internet Exploder is the browser that is doing it the old way (Chrome and Firefox get it right)

http://www.ecma-international.org/ecma-262/6.0/#sec-object.keys

note: IE11 (or whatever the abomination is called in windows 10) does it the ES2015 way

Upvotes: 3

RobG
RobG

Reputation: 147363

So does anyone know why Google Chrome implementation behaves not like in ECMAScript

It depends on which version of ECMAScript the browser has implemented.

In ECMA-262 ed 6 (the current standard) the first step is:

  1. Let obj be ToObject(O)

In ES5, the first step is:

  1. If the Type(O) is not Object, throw a TypeError exception.

So you can say that Chrome is consistent with ed 6 (it converts the primtive true to a Boolean object) and IE with ES5 (it throws a TypeError exception), and therefore both are compliant with different versions of the standard.

Upvotes: 10

Related Questions