委托util/delegate
允许动态增删要执行的函数。
基本用法
委托即函数数组。执行委托即执行数组中的每个函数。函数数组可以动态增删函数,以此增加程序的灵活性。
import Delegate from "util/delegate";
document.onclick = new Delegate(); // 创建新的委托。
document.onclick.add(function() { alert(1) }); // 添加委托函数。
document.onclick.add(function() { alert(2) }); // 添加委托函数。
API
Delegate 类
继承自:Function
表示一个委托。
字段 | 类型 | 描述 | 继承自 |
---|---|---|---|
⮞
funcs
: Function[]
获取所有委托的函数。 |
Function[]
|
获取所有委托的函数。 |
— |
⮞
prototype
: any
继承自
|
any
|
Function |
|
⮞
length
: number
(只读) 继承自
|
number
|
(只读) |
Function |
⮞
arguments
: any
继承自
|
any
|
Function |
|
⮞
caller
: Function
继承自
|
Function
|
Function |
|
⮞
name
: string
(只读)Returns the name of the function. Function names are read-only and can not be changed. 继承自
|
string
|
(只读)Returns the name of the function. Function names are read-only and can not be changed. |
Function |
方法 | 描述 | 继承自 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
⮞
new Delegate(...)(...funcs:
|
初始化新的委托。 |
— | ||||||||||||
⮞
add(func)(func:
添加一个委托函数。
返回值类型: 示例
|
添加一个委托函数。 |
— | ||||||||||||
⮞
remove(func)(func:
删除一个委托函数。
返回值类型: 示例
|
删除一个委托函数。 |
— | ||||||||||||
⮞
clear()():
删除所有委托函数。 var del = new Delegate() del.add(() => { console.log("teal") }) del.clear() del() // 控制台不输出 返回值类型: |
删除所有委托函数。 var del = new Delegate() del.add(() => { console.log("teal") }) del.clear() del() // 控制台不输出 |
— | ||||||||||||
⮞
apply(thisArg, ...)(thisArg:
Calls the function, substituting the specified object for the this value of the function, and the specified array for the arguments of the function. this:
返回值类型: 继承自
|
Calls the function, substituting the specified object for the this value of the function, and the specified array for the arguments of the function. |
Function |
||||||||||||
⮞
call(thisArg, ...)(thisArg:
Calls a method of an object, substituting another object for the current object. this:
返回值类型: 继承自
|
Calls a method of an object, substituting another object for the current object. |
Function |
||||||||||||
⮞
bind(thisArg, ...)(thisArg:
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. this:
返回值类型: 继承自
|
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. |
Function |
||||||||||||
⮞
toString()():
Returns a string representation of a function. 返回值类型: 继承自
|
Returns a string representation of a function. |
Function |
||||||||||||
⮞
[Symbol.hasInstance](value)(value:
Determines whether the given value inherits from this function if this function was used as a constructor function. A constructor function can control which objects are recognized as its instances by 'instanceof' by overriding this method.
返回值类型: 继承自
|
Determines whether the given value inherits from this function if this function was used as a constructor function. A constructor function can control which objects are recognized as its instances by 'instanceof' by overriding this method. |
Function |