1

In the following code:

const is_strict = (function() { return this === undefined});
console.log(is_strict());
{
    "use strict";
    console.log(is_strict());
}
// false false

It seems the this is always going to be within the context of where it is evaluated. Is there a way to make a single function such that it tells us whether the place where that function is invoked it is in strict mode or not? Or would it be required to inline the entire function each time:

(function() { return this === undefined})()
David542
  • 104,438
  • 178
  • 489
  • 842
  • 2
    Code is always strict or not where it is *defined*, not where it is called. – deceze Oct 18 '21 at 18:25
  • 1
    Specifically, [this answer](https://stackoverflow.com/a/62736775/215552), warning about exactly that problem. – Heretic Monkey Oct 18 '21 at 18:26
  • 1
    Why would you even need to check this dynamically? Strict mode is lexical, just look at your code to see whether you've written strict-mode code or not. – Bergi Oct 18 '21 at 19:46

0 Answers0