For instance:
function GoAlert(text){
alert(text)
setTimeout(GoAlert.bind(text),100);
}
GoAlert("Hello World");
The first alert says Hello World
, but the next ones says undefined
. Why?
When using .bind()
, the 1st argument you provide specifies the value of this
for the function.
Syntax
fun .bind( thisArg [, arg1 [, arg2 [, ...]]])
To provide a value for the 1st parameter ( text
), it should be the 2nd argument ( arg1
).
setTimeout(GoAlert.bind(undefined, text), 100);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.