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:
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.
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:
- 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 compliant 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.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With