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 .1/#sec-15.2.3.14 it says:
- 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.
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/ecma-262/5.1/#sec-15.2.3.14 it says:
- 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.
Share Improve this question asked Aug 10, 2015 at 11:07 Andzej MaciusovicAndzej Maciusovic 4,4761 gold badge31 silver badges40 bronze badges 1- 1 See code.google./p/v8/issues/detail?id=3443#c1, Also bugzilla-dev.allizom/show_bug.cgi?id=1038545j. – user663031 Commented Aug 10, 2015 at 11:22
2 Answers
Reset to default 10So 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:
- Let obj be ToObject(O)
In ES5, the first step is:
- 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 pliant with different versions of the standard.
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/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/ecma-262/6.0/#sec-object.keys
note: IE11 (or whatever the abomination is called in windows 10) does it the ES2015 way