2021-01-11 12:12:59 +08:00
|
|
|
|
## object 扩展函数
|
|
|
|
|
|
2021-01-13 14:11:48 +08:00
|
|
|
|
object 适用于 app\_conf、typed\_array 和 array 等各种对象。
|
2021-01-11 12:12:59 +08:00
|
|
|
|
|
2021-09-10 16:26:04 +08:00
|
|
|
|
### 1. object\_create
|
2021-01-11 12:12:59 +08:00
|
|
|
|
|
|
|
|
|
> 创建缺省的 object 对象,可以当一个 map 使用。
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-09-10 16:26:04 +08:00
|
|
|
|
object_create() => object
|
2021-01-11 12:12:59 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
|
var a = object_create()
|
2021-01-11 12:12:59 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-09-10 16:26:04 +08:00
|
|
|
|
### 2. object\_get
|
2021-01-11 12:12:59 +08:00
|
|
|
|
|
|
|
|
|
> 获取对象的属性。
|
2021-11-22 16:39:37 +08:00
|
|
|
|
----------------------------
|
2021-01-13 14:11:48 +08:00
|
|
|
|
|
|
|
|
|
可以通过 value\_is\_valid 来判断返回值是否有效。
|
2021-01-11 12:12:59 +08:00
|
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 14:11:48 +08:00
|
|
|
|
object_get(o, key) => value
|
2021-09-07 11:09:38 +08:00
|
|
|
|
object_get(o, key, defval) => value
|
2021-01-11 12:12:59 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-14 11:01:47 +08:00
|
|
|
|
也可以直接访问对象的属性:
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-14 11:01:47 +08:00
|
|
|
|
print(o.key)
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-11 12:12:59 +08:00
|
|
|
|
#### 示例
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-11 12:12:59 +08:00
|
|
|
|
print(object_get(a, "age"))
|
2021-01-14 11:01:47 +08:00
|
|
|
|
print(a.age)
|
2021-01-11 12:12:59 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-09-10 16:26:04 +08:00
|
|
|
|
### 3. object\_set
|
2021-01-11 12:12:59 +08:00
|
|
|
|
|
|
|
|
|
> 设置对象的属性。
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-09-10 16:26:04 +08:00
|
|
|
|
object_set(obj, key, value) => bool
|
2021-01-11 12:12:59 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-11 12:12:59 +08:00
|
|
|
|
object_set(a, "age", 123);
|
|
|
|
|
```
|
|
|
|
|
|
2021-09-10 16:26:04 +08:00
|
|
|
|
### 4. object\_remove
|
2021-01-11 12:12:59 +08:00
|
|
|
|
|
|
|
|
|
> 删除对象的属性。
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-09-10 16:26:04 +08:00
|
|
|
|
object_remove(obj, key) => bool
|
2021-01-11 12:12:59 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-11 12:12:59 +08:00
|
|
|
|
object_remove(a, "age")
|
|
|
|
|
```
|
|
|
|
|
|
2021-09-10 16:26:04 +08:00
|
|
|
|
### 5. object\_ref
|
|
|
|
|
|
|
|
|
|
> 引用计数加1。
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-09-10 16:26:04 +08:00
|
|
|
|
object_ref(obj) => object
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-09-10 16:26:04 +08:00
|
|
|
|
object_ref(a)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 5. object\_unref
|
|
|
|
|
|
|
|
|
|
> 引用计数减1。引用计数为0时,销毁对象。
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-09-10 16:26:04 +08:00
|
|
|
|
object_unref(obj)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-09-10 16:26:04 +08:00
|
|
|
|
object_ref(a)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 6. object\_forward\_events
|
|
|
|
|
|
|
|
|
|
> 将源对象的事件转发目标对象。
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
目前支持以下事件的转发:
|
|
|
|
|
|
|
|
|
|
* EVT_PROP_CHANGED:对象中的属性改变事件。
|
|
|
|
|
* EVT_PROPS_CHANGED:对象中的属性改变事件。
|
|
|
|
|
* EVT_ITEMS_CHANGED:集合对象中的项目改变事件
|
|
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-09-10 16:26:04 +08:00
|
|
|
|
object_forward_events(src_obj, dst_obj)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-09-10 16:26:04 +08:00
|
|
|
|
global = object_create()
|
|
|
|
|
global.arr = array_create()
|
|
|
|
|
object_forward_events(global.arr, global)
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-11 12:12:59 +08:00
|
|
|
|
### 完整示例
|
|
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
|
var a = object_create()
|
2021-01-11 12:12:59 +08:00
|
|
|
|
assert(!value_is_null(a))
|
|
|
|
|
assert(object_set(a, "age", 123))
|
2021-01-14 11:01:47 +08:00
|
|
|
|
assert(a.age == 123)
|
|
|
|
|
|
|
|
|
|
assert(object_set(a, "name", "awtk"))
|
|
|
|
|
assert(a.name == "awtk")
|
|
|
|
|
|
2021-01-11 12:12:59 +08:00
|
|
|
|
assert(object_get(a, "age") == 123)
|
|
|
|
|
print(object_get(a, "age"))
|
|
|
|
|
assert(object_remove(a, "age"))
|
|
|
|
|
assert(!value_is_valid(object_get(a, "age")))
|
|
|
|
|
unset(a)
|
2021-01-14 11:01:47 +08:00
|
|
|
|
|
2022-08-23 10:34:55 +08:00
|
|
|
|
var person = object_create()
|
2021-01-14 11:01:47 +08:00
|
|
|
|
assert(!value_is_null(person))
|
|
|
|
|
|
|
|
|
|
assert(object_set(person, "age", 123))
|
|
|
|
|
assert(person.age == 123)
|
|
|
|
|
|
|
|
|
|
assert(object_set(person, "name", "awtk"))
|
|
|
|
|
assert(person.name == "awtk")
|
|
|
|
|
|
2022-08-23 10:34:55 +08:00
|
|
|
|
var g = object_create()
|
|
|
|
|
g.arr = array_create()
|
|
|
|
|
object_forward_events(g.arr, g)
|
2021-01-11 12:12:59 +08:00
|
|
|
|
```
|