JS:绑定函数无法在递归中传递参数
JS: bind function fail passing arguments in recursions
例如:
function GoAlert(text){
alert(text)
setTimeout(GoAlert.bind(text),100);
}
GoAlert("Hello World");
第一个警报显示 Hello World
,但接下来的警报显示 undefined
。为什么?
使用 .bind()
时,您提供的第一个参数指定函数的 this
值。
Syntax
<em>fun</em>.bind(<em>thisArg</em>[, <em>arg1</em>[, <em>arg2</em>[, ...]]])
要为第一个参数 (text
) 提供值,它应该是第二个参数 (arg1
)。
setTimeout(GoAlert.bind(undefined, text), 100);
例如:
function GoAlert(text){
alert(text)
setTimeout(GoAlert.bind(text),100);
}
GoAlert("Hello World");
第一个警报显示 Hello World
,但接下来的警报显示 undefined
。为什么?
使用 .bind()
时,您提供的第一个参数指定函数的 this
值。
Syntax
<em>fun</em>.bind(<em>thisArg</em>[, <em>arg1</em>[, <em>arg2</em>[, ...]]])
要为第一个参数 (text
) 提供值,它应该是第二个参数 (arg1
)。
setTimeout(GoAlert.bind(undefined, text), 100);