Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 434
Andzej Maciusovic Avatar asked Aug 10 '15 11:08

Andzej Maciusovic


2 Answers

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.

like image 186
RobG Avatar answered Oct 02 '22 01:10

RobG


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

like image 26
Jaromanda X Avatar answered Oct 02 '22 01:10

Jaromanda X