老師bind文檔用法看不太懂

来源:4-2 React 中的前端路由(2)

weixin_慕雪0272820

2020-09-13 15:51:49

(method) Function.bind(this: Function, thisArg: any, ...argArray: any[]): any

For a given function, creates a bound function that has the same body as the original function. The this object of the bound function is associated with the specified object, and has the specified initial parameters.

@param thisArg — An object to which the this keyword can refer inside the new function.

@param argArray — A list of arguments to be passed to the new function.

以上是我在vscode找到的bind用法文檔,老師可不可以解說下文檔的意思?

(this: Function, thisArg: any, ...argArray: any[]): any

尤其是以上這句話代表什麼意思?

如果用this.handleDeleteItem.bind(this,index) 傳入index是可以的嗎?

文檔內看起來是說只能傳對象?An object to which the this keyword can refer inside the new function.


写回答

1回答

慕课网的粉

2020-09-13

你看的那个有一点复杂了,你看下这个:

this.handleclick.bind(this,要传的参数)  handleclick(传过来的参数,event)

事件–this.handleclick.bind(this,要传的参数)
函数–handleclick(传过来的参数,event)/ handleclick(传过来的参数)

(this: Function, thisArg: any, ...argArray: any[]): any   这句话其实描述的就是它的参数,第一个表示this,可以指代当前组件;后面thisArg是函数接受的参数,可以有多个thisArg。
可以传入index了,handleDeleteItem函数调用时候传递index即可!

“An object to which the this keyword can refer inside the new function.”这句话是说this,可以引用到新函数的里对象~~~

0

0 学习 · 10739 问题

查看课程