鍍金池/ 問答/HTML/ js 嚴(yán)格模式下arguments.callee用什么替代

js 嚴(yán)格模式下arguments.callee用什么替代

如下所示是一個定時器對象,在嚴(yán)格模式下會報錯,有木有大佬知道怎么修改啊?

var Timing = function(fun,interval){
    this.fun = fun;
    this.interval = interval*1000;
    this.timer;
}
Timing.prototype = {
    constructor: Timing,
    setTime: function(){
        var that = this;
        var fun = that.fun;
        that.timer = setTimeout(function(){
            fun();
            that.timer = setTimeout(arguments.callee,that.interval);
        },that.interval);
    },
    clearTime: function(){
        clearTimeout(this.timer)
    }
}

實例化
var time = new Timing(function(){
    console.log('hello');
},1);
time.setTime();

控制臺報錯:Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them

回答
編輯回答
話寡

代替是不可能的,但是為什么要用匿名函數(shù)呢。

        that.timer = setTimeout(function cb(){
            fun();
            that.timer = setTimeout(cb,that.interval);
        },that.interval);
2017年7月16日 05:08
編輯回答
柚稚

沒有替代。

2018年8月27日 08:41
編輯回答
扯不斷

除此之外,建議學(xué)習(xí)使用 ES6 的 let 和箭頭函數(shù),代替 var 和 that。

2018年6月25日 22:23