Constructor
new BaseComponentGroup()
The base component group to manage components, you should inherit from it when you want to manage multiple components.
- Source:
Members
(readonly) components :Map.<String, THING.BaseComponent>
Get all components.
Type:
- Map.<String, THING.BaseComponent>
- Source:
Example
let obj = new THING.BaseObject();;
obj.addComponent(new THING.BaseComponent(), 'component1');
obj.addComponent(new THING.BaseComponent(), 'component2');
// @expect(obj.components.size == 2)
Methods
addComponent(component, name, args?) → {Boolean}
Add component.
Parameters:
Name | Type | Description |
---|---|---|
component |
THING.BaseComponent | Object | The component class or component object. |
name |
String | The name. |
args? |
Object | The initial arguments to create component. |
- Source:
Returns:
- Type
- Boolean
Example
let obj = new THING.BaseObject();;
obj.addComponent(new THING.BaseComponent(), 'myComponent');
// @expect(obj.myComponent != null)
getAllComponents() → {Array.<THING.BaseComponent>}
Get all components(it would create all registered components).
- Source:
Returns:
- Type
- Array.<THING.BaseComponent>
Example
let obj = new THING.BaseObject();;
obj.addComponent(new THING.BaseComponent(), 'component1');
obj.addComponent(new THING.BaseComponent(), 'component2');
let components = obj.getAllComponents();
// @expect(components.length == 2)
getComponentByName(name) → {THING.BaseComponent}
Get component by name.
Parameters:
Name | Type | Description |
---|---|---|
name |
String | The name. |
- Source:
Returns:
- Type
- THING.BaseComponent
Example
let obj = new THING.BaseObject();;
obj.addComponent(new THING.BaseComponent(), 'myComponent');
let component = obj.getComponentByName('myComponent');
// @expect(component != null)
getComponentByType(type) → {THING.BaseComponent}
Get component by type.
Parameters:
Name | Type | Description |
---|---|---|
type |
* | The component type. |
- Source:
Returns:
- Type
- THING.BaseComponent
Example
let obj = new THING.BaseObject();;
obj.addComponent(new THING.BaseComponent(), 'myComponent');
let component = obj.getComponentByType(THING.BaseComponent);
// @expect(component != null)
getComponentsByType(type) → {Array.<THING.BaseComponent>}
Get components by type.
Parameters:
Name | Type | Description |
---|---|---|
type |
* | The component type. |
- Source:
Returns:
- Type
- Array.<THING.BaseComponent>
Example
let obj = new THING.BaseObject();;
obj.addComponent(new THING.BaseComponent(), 'component1');
obj.addComponent(new THING.BaseComponent(), 'component2');
let components = obj.getComponentsByType(THING.BaseComponent);
// @expect(components.length == 2)
hasComponent(name) → {Boolean}
Check whether has component.
Parameters:
Name | Type | Description |
---|---|---|
name |
String | The name. |
- Source:
Returns:
- Type
- Boolean
Example
let obj = new THING.BaseObject();;
obj.addComponent(new THING.BaseComponent(), 'myComponent');
let ret = obj.hasComponent('myComponent')
// @expect(ret == true)
removeAllComponents()
Remove all components.
- Source:
Example
let obj = new THING.BaseObject();;
obj.addComponent(new THING.BaseComponent(), 'component1');
obj.addComponent(new THING.BaseComponent(), 'component2');
obj.removeAllComponents();
// @expect(obj.components.size == 0)
removeComponent(name)
Remove component.
Parameters:
Name | Type | Description |
---|---|---|
name |
String | The name. |
- Source:
Example
let obj = new THING.BaseObject();;
obj.addComponent(new THING.BaseComponent(), 'myComponent');
obj.removeComponent('myComponent');
// @expect(obj.components.size == 0)
traverseComponentByType(type, callback)
Traverse component by type.
Parameters:
Name | Type | Description |
---|---|---|
type |
* | The component type. |
callback |
TraverseComponentByTypeCallback | The callback function. |
- Source:
Example
object.addComponent(new MyComponent(), 'myComponent');
object.traverseComponentByType(MyComponent, (component, name) => {
console.log(component, name);
});