Object.keys behavior in chrome and IE 11

Today I received an error using Object.keys because I accidentally passed a value without an object:

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

This works fine in Chrome, but in IE 11 I got an exception and after debugging I found that Object.keys throws an Object.keys exception in IE 11 : the argument is not an object .

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

  • If type (O) is not an object, throw a TypeError exception.

Does anyone know why the implementation of Google Chrome behaves differently than in the ECMAScript specification standard, because I always thought that all modern browsers should implement ECMAScript in order to behave the same way.

+7
javascript google-chrome internet-explorer
source share
2 answers

Does anyone know why the behavior of Google Chrome does not work in the ECMAScript implementation,

It depends on which version of ECMAScript is implemented by the browser.

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

  • Let obj be TOObject (O)

In ES5, the first step:

  • If type (O) is not an object, throw a TypeError exception.

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

+4
source share

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

If type (O) is not an object, throw a TypeError exception.

ES5 old

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

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

Note: IE12 (or something that causes nasty things in windows 10) does it in ES2015 way

+2
source share

All Articles