为什么我们不能在 setTimeout 上打电话和申请? [英] why can't we do call and apply on setTimeout?

查看:50
本文介绍了为什么我们不能在 setTimeout 上打电话和申请?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们不能在 setTimeout 上调用和申请,

Why can't we do call and apply on setTimeout,

var obj={}
window.setTimeout.call(obj,callback,delay);//it throws error stating illegal invocation 

推荐答案

来自 WHATWG setTimeout 文档:

setTimeout() 方法必须返回定时器初始化步骤返回的值,将方法的参数、算法运行的方法在其上实现的对象(Window 或 WorkerGlobalScope 对象)传递给它们方法上下文,并将重复标志设置为 false.

The setTimeout() method must return the value returned by the timer initialisation steps, passing them the method's arguments, the object on which the method for which the algorithm is running is implemented (a Window or WorkerGlobalScope object) as the method context, and the repeat flag set to false.

setTimeout 需要从 window 对象的上下文中调用.传递给 .call 方法的上下文不是 window 对象.要正确调用 setTimeout,请执行以下操作:

setTimeout needs to be called from the context of the window object. The context passed into the .call method is not the window object. To call setTimeout correctly, do:

setTimeout.call(window, callback, delay);

这篇关于为什么我们不能在 setTimeout 上打电话和申请?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆