3

In this MDN document about the Strict Mode of JavaScript, under "Semantic differences -> this in function calls", it's mentioned that:

When a function was called with call or apply, if the value was a primitive value, this one was boxed into an object (or the global object for undefined and null). In strict mode, the value is passed directly without conversion or replacement.

I need clarification for this statement. When I test this, I don't see any difference based on the mode (strict or sloppy) of the code.

Please let me know if I have misunderstood the statement.

This is how I've tested:

(function() { function a() { console.log(this); } function b() { "use strict"; a.call(2); } function c() { a.call(3); } b(); c(); })();

Result :

enter image description here

1
  • 2
    It works as expected if you put "use strict"; at top-level, the beginning of the IIFE, or in a(). But not if you put it in b() or c(). Not sure why. Commented Jul 7, 2021 at 14:46

1 Answer 1

5

The behavior being described depends on whether the function being called is in strict mode, not the caller.

(function() { function strict() { "use strict"; console.log(this); } function sloppy() { console.log(this); } function b() { strict.call(2); } function c() { sloppy.call(3); } b(); c(); })();

Normally you put an entire script into strict mode, so the distinction between caller and callee doesn't matter.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer. I think I've misunderstood the statement. Now it makes sense.
The wording in MDN is somewhat ambiguous.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.