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
|
|
|
|
|
|
|
### 1. object_create
|
|
|
|
|
|
|
|
> 创建缺省的 object 对象,可以当一个 map 使用。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```
|
2021-01-13 14:11:48 +08:00
|
|
|
object_create() => value
|
2021-01-11 12:12:59 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```
|
|
|
|
a = object_create()
|
|
|
|
```
|
|
|
|
|
|
|
|
### 2. object_get
|
|
|
|
|
|
|
|
> 获取对象的属性。
|
2021-01-13 14:11:48 +08:00
|
|
|
|
|
|
|
可以通过 value\_is\_valid 来判断返回值是否有效。
|
2021-01-11 12:12:59 +08:00
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```
|
2021-01-13 14:11:48 +08:00
|
|
|
object_get(o, key) => value
|
2021-01-11 12:12:59 +08:00
|
|
|
```
|
|
|
|
|
2021-01-14 11:01:47 +08:00
|
|
|
也可以直接访问对象的属性:
|
|
|
|
|
|
|
|
```
|
|
|
|
print(o.key)
|
|
|
|
```
|
|
|
|
|
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
|
|
|
```
|
|
|
|
|
|
|
|
### 3. object_set
|
|
|
|
|
|
|
|
> 设置对象的属性。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```
|
2021-01-13 14:11:48 +08:00
|
|
|
object_set(a, key, value) => bool
|
2021-01-11 12:12:59 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```
|
|
|
|
object_set(a, "age", 123);
|
|
|
|
```
|
|
|
|
|
|
|
|
### 4. object_remove
|
|
|
|
|
|
|
|
> 删除对象的属性。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```
|
2021-01-13 14:11:48 +08:00
|
|
|
object_remove(a, key) => bool
|
2021-01-11 12:12:59 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```
|
|
|
|
object_remove(a, "age")
|
|
|
|
```
|
|
|
|
|
|
|
|
### 完整示例
|
|
|
|
|
|
|
|
```js
|
|
|
|
a = object_create()
|
|
|
|
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
|
|
|
|
|
|
|
person = object_create()
|
|
|
|
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")
|
|
|
|
|
|
|
|
unset(person)
|
|
|
|
|
|
|
|
|
2021-01-11 12:12:59 +08:00
|
|
|
```
|