1

Here is the code from MDN:

function f2(){
  "use strict"; // see strict mode
  return this;
}

console.log(f2());

f2 call in script works fine. But its call from console shows window object! What's wrong? Setting "javascript.options.strict" option doesn't help.

enter image description here

In Chrome everything works fine.

Dmitriy Dokshin
  • 638
  • 5
  • 22
  • That's right, it looks like a bug. Interestingly launching a some bare index.html that includes your example.js will apply "use strict" displaying undefined like it should be instead of an archaic global object. – daGo Dec 26 '16 at 18:01

1 Answers1

1

If you really did execute the exact MDN snippet, it would be a bug, that must have been already fixed because I can't reproduce it.

However, one should note that if we refer to this answer and try to execute the tests in separated statements in firefox console:

>> var isStrict = (function() { return !this; })(); console.log(isStrict);
false
>> "use strict"; var isStrict = (function() { return !this; })(); console.log(isStrict);
true 

This proves that Firefox's Dev tools do take "use strict" into account. Now if we try one more:

>> "use strict";
"use strict"
>> var isStrict = (function() { return !this; })(); console.log(isStrict);
false

What changed here is that "use strict" was used in a separated prompt rather than on the same one, and it would seems that Firefox's dev tools reinitialize strict mode for each prompt. As a consequence if instead of using exactly the wording of MDN you tried something like

  • issuing "use strict" on the first prompt,
  • then declaring your function on the second line
  • and finally displayed it on a third line as we can see on the screenshot,

...the output would have been normal regarding the fact that "use strict" is reset on each prompt.

NB: note that on the other hand, the following seems to be working:

>> "use strict"; function f3(){return this;}
"use strict"
>> console.log(f3())
undefined
Aldian
  • 2,481
  • 2
  • 24
  • 38