Home Reference Source Repository
import Collection from 'emc/src/Collection.js'
public class | source

Collection

Extends:

* → Collection

一个带有集合变更通知的数组

Constructor Summary

Public Constructor
public

constructor(items: *)

Member Summary

Public Members
public

Method Summary

Public Methods
public

add(item: *, options: Object): *

在当前集合的最后位置添加一个元素

public

addAt(index: number, item: *, options: Object): *

在指定位置添加一个元素,与Collection#insert方法相同

public

复制当前集合

public

销毁当前集合

public

dump(): Array

导出当前集合为普通的数组

public

get(index: number): *

获取集合中指定位置上的元素

public

indexOf(item: *, startIndex: number): number

查找指定元素在集合中第一次出现的位置

public

insert(index: number, item: *, options: Object): *

在指定位置添加一个元素

public

pop(options: Object): *

移除集合最后一个元素并返回

public

push(item: *, options: Object): *

在当前集合的最后位置添加一个元素,与Collection#add方法相同

public

remove(item: *, options: Object)

移除集合中所有的给定元素

public

removeAt(index: number, options: Object)

从指定位置移除一个元素

public

shift(options: Object): *

移除集合第一个元素并返回

public

unshift(item: *, options: Object): *

在当前集合的最前位置添加一个元素

Protected Methods
protected

addArray(items: Array, options: Object)

添加一系列的元素

protected

获取一个可用的索引值

Public Constructors

public constructor(items: *) source

Params:

NameTypeAttributeDescription
items *

Public Members

public length: number source

Public Methods

public add(item: *, options: Object): * source

在当前集合的最后位置添加一个元素

Params:

NameTypeAttributeDescription
item *

需要添加的元素

options Object
  • optional

相关选项

options.silent boolean
  • optional
  • default: false

如果该值为true则不触发{@link Collection#event:add|add事件}

Return:

*

插入的元素

Throw:

Error

当前集合已经销毁

Error

未提供item参数

public addAt(index: number, item: *, options: Object): * source

在指定位置添加一个元素,与Collection#insert方法相同

Params:

NameTypeAttributeDescription
index number

需要添加的位置,关于位置的计算参考Collection#getValidIndex

item *

需要添加的元素

options Object
  • optional

相关选项

options.silent boolean
  • optional
  • default: false

如果该值为true则不触发{@link Collection#event:add|add事件}

Return:

*

插入的元素

Throw:

Error

当前集合已经销毁

Error

未提供index参数

Error

提供的index参数无法转换为数字

Error

未提供item参数

public clone(): Collection source

复制当前集合

Return:

Collection

一个新的集合,包含当前集合的元素(及其顺序)

Throw:

Error

当前集合已经销毁

public dispose() source

销毁当前集合

public dump(): Array source

导出当前集合为普通的数组

如果当前集合已经销毁,该方法会返回一个空数组[]

Return:

Array

包含当前集合的元素(及其顺序)的数组

public get(index: number): * source

获取集合中指定位置上的元素

Params:

NameTypeAttributeDescription
index number

指定位置,如果为负数则从元素最后开始往前计算

Return:

*

指定位置的元素,如果指定的位置超出集合范围,则返回undefined

Throw:

Error

当前集合已经销毁

Error

未提供index参数

Error

提供的index参数无法转换为数字

public indexOf(item: *, startIndex: number): number source

查找指定元素在集合中第一次出现的位置

Params:

NameTypeAttributeDescription
item *

指定查找的元素

startIndex number
  • optional
  • default: 0

指定开始查找的位置,如果为负数则从最后位置往前计算,如果超出范围则不进行搜索返回-1

Return:

number

元素所在的位置,如果集合中从指定的位置开始未能找到元素则返回-1

Throw:

Error

当前集合已经销毁

Error

未提供item参数

Error

提供的startIndex参数无法转换为数字

public insert(index: number, item: *, options: Object): * source

在指定位置添加一个元素

Params:

NameTypeAttributeDescription
index number

需要添加的位置,关于位置的计算参考Collection#getValidIndex

item *

需要添加的元素

options Object
  • optional

相关选项

options.silent boolean
  • optional
  • default: false

如果该值为true则不触发Collection#event:add事件

Return:

*

插入的元素

Throw:

Error

当前集合已经销毁

Error

未提供index参数

Error

提供的index参数无法转换为数字

Error

未提供item参数

public pop(options: Object): * source

移除集合最后一个元素并返回

Params:

NameTypeAttributeDescription
options Object
  • optional

相关选项

options.silent boolean
  • optional
  • default: false

如果该值为true则不触发{@link Collection#event:remove|remove事件}

Return:

*

集合中的最后一个元素

Throw:

Error

当前集合已经销毁

public push(item: *, options: Object): * source

在当前集合的最后位置添加一个元素,与Collection#add方法相同

Params:

NameTypeAttributeDescription
item *

需要添加的元素

options Object
  • optional

相关选项

options.silent boolean
  • optional
  • default: false

如果该值为true则不触发{@link Collection#event:add|add事件}

Return:

*

插入的元素

Throw:

Error

当前集合已经销毁

Error

未提供item参数

public remove(item: *, options: Object) source

移除集合中所有的给定元素

Params:

NameTypeAttributeDescription
item *

需要移除的元素

options Object
  • optional

相关选项

options.silent boolean
  • optional
  • default: false

如果该值为true则不触发{@link Collection#event:remove|remove事件}

Throw:

Error

当前集合已经销毁

Error

未提供item参数

public removeAt(index: number, options: Object) source

从指定位置移除一个元素

Params:

NameTypeAttributeDescription
index number

需要移除的元素的位置,关于位置的计算参考Collection#getValidIndex

options Object
  • optional

相关选项

options.silent boolean
  • optional
  • default: false

如果该值为true则不触发{@link Collection#event:remove|remvoe事件}

Throw:

Error

当前集合已经销毁

Error

未提供index参数

Error

提供的index参数无法转换为数字

public shift(options: Object): * source

移除集合第一个元素并返回

Params:

NameTypeAttributeDescription
options Object
  • optional

相关选项

options.silent boolean
  • optional
  • default: false

如果该值为true则不触发{@link Collection#event:remove|remove事件}

Return:

*

集合中的第一个元素

Throw:

Error

当前集合已经销毁

public unshift(item: *, options: Object): * source

在当前集合的最前位置添加一个元素

Params:

NameTypeAttributeDescription
item *

需要添加的元素

options Object
  • optional

相关选项

options.silent boolean
  • optional
  • default: false

如果该值为true则不触发{@link Collection#event:add|add事件}

Return:

*

插入的元素

Throw:

Error

当前集合已经销毁

Error

未提供item参数

Protected Methods

protected addArray(items: Array, options: Object) source

添加一系列的元素

此方法为私有方法,不要由外部或子类调用

Params:

NameTypeAttributeDescription
items Array

需要添加的元素数组

options Object
  • optional

相关选项

options.silent boolean
  • optional
  • default: false

如果该值为true则不触发Collection#event:add事件

Throw:

Error

提供的items参数不是数组

protected getValidIndex(index: number): number source

获取一个可用的索引值

  • 当给定的索引大于集合长度时,返回集合的长度
  • 当给定的索引小于0时,从集合的末尾开始向前计算
  • 当索引小于0并且其绝对值大于集合长度时,返回0

如果用于删除,由于索引值过大时会返回当前集合的长度,需要在返回值之后再减去1得到正确的删除位置

Params:

NameTypeAttributeDescription
index number

输入的索引值

Return:

number

计算后的可用索引值

Throw:

Error

未提供index参数

Error

提供的index参数无法转换为数字