mirror of
https://github.com/denoland/deno.git
synced 2025-01-08 15:19:40 -05:00
set setTimeout callback's this to window object
This commit is contained in:
parent
0efb7c2ab7
commit
cb58162052
2 changed files with 15 additions and 2 deletions
|
@ -3,6 +3,7 @@ import { assert } from "./util";
|
|||
import * as msg from "gen/cli/msg_generated";
|
||||
import * as flatbuffers from "./flatbuffers";
|
||||
import { sendAsync, sendSync } from "./dispatch";
|
||||
import { window } from "./window";
|
||||
|
||||
interface Timer {
|
||||
id: number;
|
||||
|
@ -186,8 +187,8 @@ function setTimer(
|
|||
args: Args,
|
||||
repeat: boolean
|
||||
): number {
|
||||
// If any `args` were provided (which is uncommon), bind them to the callback.
|
||||
const callback: () => void = args.length === 0 ? cb : cb.bind(null, ...args);
|
||||
// Bind `args` to the callback and bind `this` to window(global).
|
||||
const callback: () => void = cb.bind(window, ...args);
|
||||
// In the browser, the delay value must be coercible to an integer between 0
|
||||
// and INT32_MAX. Any other value will cause the timer to fire immediately.
|
||||
// We emulate this behavior.
|
||||
|
|
|
@ -165,3 +165,15 @@ test(async function fireCallbackImmediatelyWhenDelayOverMaxValue(): Promise<
|
|||
await waitForMs(1);
|
||||
assertEquals(count, 1);
|
||||
});
|
||||
|
||||
test(async function timeoutCallbackThis(): Promise<void> {
|
||||
const { promise, resolve } = deferred();
|
||||
const obj = {
|
||||
foo(): void {
|
||||
assertEquals(this, window);
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
setTimeout(obj.foo, 1);
|
||||
await promise;
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue