0

I cannot figure it out, why do some browsers allow the next code, and others do not:

'use strict';
(function() {
  Number = 1;
  toString = 1;
  valueOf = 1;
})();
console.log(Number);
console.log(toString);
console.log(valueOf);
  • Chrome 52 on Windows throws on toString and valueOf, but allows Number.
  • Chrome 49 on Linux throws on valueOf, and allows others.
  • Firefox 47 allows all.
  • IE 11 allows Number.
  • Opera allows Number.

At first, when I spotted this in Chrome, I thought, this is because of these "variables" had been already defined as properties of the global object. But window.hasOwnProperty('toString') returns false, as well as for 'valueOf'. So I have no idea.

Could anyone explain this, please?

I'm asking because I need to test in unit tests, that global variables were not modified and this behavior makes it hard, because I do not know what behavior is the correct one so I could rely on it and not to find one day that it was fixed.

d.k
  • 4,234
  • 2
  • 26
  • 38
  • `window.hasOwnProperty('toString')` tests if `window` has an *own* property, however, this is (prototypal) inherited from `Object`. – fdomig Aug 02 '16 at 15:26
  • @fdomig, I know this, that is why I thought that `use strict` forbids everything, which was not declared/assigned as an own property of the global object – d.k Aug 02 '16 at 15:28

0 Answers0