Teal TealUI

委托util/delegate

允许动态增删要执行的函数。

基本用法
API
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

继承自

Function

any Function
length : number

(只读)

继承自

Function

number

(只读)

Function
arguments : any

继承自

Function

any Function
caller : Function

继承自

Function

Function Function
name : string

(只读)Returns the name of the function. Function names are read-only and can not be changed.

继承自

Function

string

(只读)Returns the name of the function. Function names are read-only and can not be changed.

Function
方法 描述 继承自
new Delegate(...)(...funcs:Function[]):Delegate

初始化新的委托。

参数 类型 描述 默认值
funcs Function[]

返回值

类型:Delegate

初始化新的委托。

add(func)(func:Function):void

添加一个委托函数。

参数 类型 描述 默认值
func* Function

返回值

类型:void

示例

var del = new Delegate()
del.add(() => { console.log("teal") })
del() // 控制台输出 teal

添加一个委托函数。

remove(func)(func:Function):void

删除一个委托函数。

参数 类型 描述 默认值
func* Function

返回值

类型:void

示例

var fn = () => { console.log("teal") }
var del = new Delegate()
del.add(fn)
del.remove(fn)
del() // 控制台不输出

删除一个委托函数。

clear()():void

删除所有委托函数。 var del = new Delegate() del.add(() => { console.log("teal") }) del.clear() del() // 控制台不输出

返回值

类型:void

删除所有委托函数。 var del = new Delegate() del.add(() => { console.log("teal") }) del.clear() del() // 控制台不输出

apply(thisArg, ...)(thisArg:any, argArray?:any):any

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:Function

参数 类型 描述 默认值
thisArg* any
argArray any

返回值

类型:any

继承自

Function

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:any, ...argArray:any[]):any

Calls a method of an object, substituting another object for the current object.

this:Function

参数 类型 描述 默认值
thisArg* any
argArray any[]

返回值

类型:any

继承自

Function

Calls a method of an object, substituting another object for the current object.

Function
bind(thisArg, ...)(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.

this:Function

参数 类型 描述 默认值
thisArg* any
argArray any[]

返回值

类型:any

继承自

Function

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()():string

Returns a string representation of a function.

返回值

类型:string

继承自

Function

Returns a string representation of a function.

Function
[Symbol.hasInstance](value)(value:any):boolean

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.

参数 类型 描述 默认值
value* any

返回值

类型:boolean

继承自

Function

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