Why does a callback in setTimeout have this == Window even in strict mode?

2024/2/27 9:07:16

I am learning JS now and can not understand why 'use strict' directive does not work for a callback for setTimeout? As I know for use strict it should be undefined but always have global object.

function f() {'use strict'console.log(this === window)
}function g() {console.log(this === window)
}setTimeout(g,1000) // expect true, have true
setTimeout(f,1000) // expect false, have true

Answer

When setTimout() invokes a function in the browser, it sets the this to window. That is why it is not affected by strict mode - the this value is provided, instead of being left unset.

The specification for setTimeout can be found in the HTML standard

The timer initialization steps, given a WindowOrWorkerGlobalScope global, a string or Function handler, a number timeout, a list arguments, a boolean repeat, and optionally (and only if repeat is true) a number previousId, are:

1. Let thisArg be global if that is a WorkerGlobalScope object; otherwise let thisArg be the WindowProxy that corresponds to global.

[...]

9. Let task be a task that runs the following substeps:

  1. If id does not exist in global's map of active timers, then abort these steps.

  2. If handler is a Function, then invoke handler given arguments with the callback this value set to thisArg. If this throws an exception, catch it, and report the exception.

[...]

In essence, the result is similar to calling the function like so:

function f(){'use strict'console.log(this)
}f.call(window)

http://en.ppmy.cn/q/41598.html

Related Q&A

Why windows.onload is executed several times?

Im binding the window.onload event like this// Its a little more complex than this, I analyze if there is any other function // attached but for the sake of the question its ok, this behaves the same.…

Get a variable after ajax done

I have this code for make some request to my server:function myAjaxCheck(token) {$.ajax({type: POST,url: auth.php,data: {token: token,},dataType: json,success: function (data) {if (data.auth == OK) {al…

Web cryptography implement HKDF for the output of ECDH

I want implement a elliptic curve diffie hellman using HKDF as key derivation function. I am using a python backend and (vanilla) javascript in frontend. I am using python cryptography library in backe…

Blurring borders in SVG (Raphael.js)

Im working on a web based editor where one can drag and resize elements. Ive faces a problem regarding displaying 1px borders and elements: instead of pixel-to-pixel displaying (e.g. 1px solid line) I …

Creating an iframe in separate process so it doesnt block the main thread of the parent window

I recently heard about the rel="noopener" attribute value that can be added to anchor tags so that the new window runs in a separate process. That got me wondering: Is it possible to create a…

Change sails.js EJS views to use .html extensions instead of .ejs extensions?

Is it possible to configure sails.js apps to use .html extentions rather than .ejs (but still use the ejs view engine)?sails new app creates ./views/home/index.ejs and ./views/layout.ejs.Id like to ch…

How to select text with jQuery

How can I select a part of the text and process by jQuery? For example, I have a text as<div id="test">This is an example text here</div>I select few words (not the whole div) wi…

jquery Using ranges in switch cases?

Switch cases are usually like Monday: Tuesday: Wednesday: etc. I would like to use ranges. from 1-12: from 13-19: from 20-21: from 22-30:Is it possible? Im using javascript/jquery by the way.

How to download a recording using getUsermedia and mediaRecorder and give the video specifications?

navigator.mediaDevices.getUserMedia().then(stream=>{//a recorder is created var mediaRecorder = new MediaRecorder(stream);//started it mediaRecorder.start();//an array is created that receives all t…

Crockfords hanoi function (from The Good Parts) [duplicate]

This question already has answers here:How does recursive algorithm work for Towers of Hanoi?(2 answers)Closed 9 years ago.At the moment Im reading Douglas Crockfords book, and the towers of hanoi fun…