A
A
Alexander Kositsyn2018-02-08 20:44:31
JavaScript
Alexander Kositsyn, 2018-02-08 20:44:31

Why is use strict not working in setTimeout?

Here is a link to the sandbox
Here is the code that is on the link.

<!DOCTYPE html>
<html>

<body>
  <script>
    'use strict';
    function func() {
      "use strict";
      alert(this ); // выведет undefined (кроме IE9-)
    }
    
    setTimeout(func,0);
  </script>
</body>

</html>

Why does this output as window? and not undefined?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-02-08
@alex_keysi

Because setTimeout calls the callback in the context of window .
Something like: It is worth adding that when the script is executed by the worker, this in the setTimeout callback will refer to the WorkerGlobalScope .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question