bind函数递归传递参数失败

JS: bind function fail passing arguments in recursions

本文关键字:参数 失败 函数 递归 bind      更新时间:2023-09-26

例如:

function GoAlert(text){
    alert(text)
    setTimeout(GoAlert.bind(text),100);
}
GoAlert("Hello World");

第一个警报是Hello World,但下一个警报是undefined。为什么?

当使用.bind()时,您提供的第一个参数指定该函数的this值。

语法

fun.bind(thisArg[, arg1[, arg2[, ...]]])

为第一个参数(text)提供一个值,它应该是第二个参数(arg1)。

setTimeout(GoAlert.bind(undefined, text), 100);