Object 对象类
Object 是 Alia 的内置对象类,用于存储键值对集合。Alia 的对象系统提供了丰富的属性操作和方法。
创建对象
使用字面量
const obj1 = *[name: 'Alice', age: 25]
const obj2 = *[
name: 'Bob',
age: 30,
address: *[
city: 'Beijing',
country: 'China'
]
]
使用构造函数
const seed = *[name: 'Alice']
const obj3 = Object.create seed
const entries = [['name', 'Alice'], ['age', 25]]
const obj4 = Object.fromEntries entries
从 JSON 文件导入
Alia 支持通过 import 语句直接从 JSON 文件导入数据,自动转换为 Object 类型。
~ config.json 文件内容:
~ { "name": "Alice", "age": 30, "isStudent": true, "scores": [95, 88, 92] }
import 'config.json' as config
print Object.get config keyed 'name' ~ 输出 'Alice'
print Object.get config keyed 'age' ~ 输出 30
print Object.get config keyed 'isStudent' ~ 输出 true
特性:
- JSON 对象自动转换为 Alia
Object - JSON 数组自动转换为 Alia
Array - JSON 基本类型(字符串、数字、布尔值、null)自动转换为对应的 Alia 类型
- 支持嵌套结构
方法
Object 的属性操作统一通过静态方法调用,以对象作为第一个参数传入。
Object.create
创建新对象。接收单个值参数并返回新对象。
const seed = *[name: 'Alice']
const obj = Object.create seed
Object.fromEntries
从键值对数组创建对象。接收单个数组参数并返回新对象。
const entries = [['name', 'Alice'], ['age', 25]]
const obj = Object.fromEntries entries
print obj ~ 输出 *[name: Alice, age: 25]
Object.assign
合并多个对象。目标之后以 with 连接各来源对象,来源数量可变,返回合并后的新对象。
const obj1 = *[name: 'Alice']
const obj2 = *[age: 25]
const obj3 = *[city: 'Beijing']
const merged = Object.assign obj1 with obj2 with obj3
print merged ~ 输出 *[name: Alice, age: 25, city: Beijing]
Object.keys
获取对象的属性名数组。接收单个对象参数并返回数组。
const obj = *[name: 'Alice', age: 25]
print Object.keys obj ~ 输出 ['name', 'age']
Object.values
获取对象的属性值数组。接收单个对象参数并返回数组。
const obj = *[name: 'Alice', age: 25]
print Object.values obj ~ 输出 ['Alice', 25]
Object.entries
获取对象的键值对数组。接收单个对象参数并返回数组。
const obj = *[name: 'Alice', age: 25]
print Object.entries obj ~ 输出 [['name', 'Alice'], ['age', 25]]
Object.contact
合并两个对象。以 with 连接左右对象,返回合并后的新对象。
const obj1 = *[name: 'Alice']
const obj2 = *[age: 25]
const merged = Object.contact obj1 with obj2
print merged ~ 输出 *[name: Alice, age: 25]
Object.isObject
检查值是否为 Alia 对象。接收单个值参数并返回布尔值。
const obj = *[name: 'Alice']
const arr = [1, 2, 3]
print Object.isObject obj ~ 输出 true
print Object.isObject arr ~ 输出 false
Object.clone
创建对象的浅拷贝。接收单个对象参数并返回新对象。
const original = *[name: 'Alice', age: 25]
const copy = Object.clone original
print copy ~ 输出 *[name: Alice, age: 25]
Object.set
设置对象的属性。以 keyed 指定键并以 to 指定值,返回传入的对象本身。
const obj = *[name: 'Alice']
Object.set obj keyed 'age' to 25
print obj ~ 输出 *[name: Alice, age: 25]
Object.get
获取对象的属性。以 keyed 指定键并以 or 指定默认值,默认值缺省时为 Null,返回属性值或默认值。
const obj = *[name: 'Alice']
print Object.get obj keyed 'name' ~ 输出 'Alice'
print Object.get obj keyed 'age' or 0 ~ 输出 0
Object.has
检查对象是否包含指定属性。以 keyed 指定键,返回布尔值。
const obj = *[name: 'Alice', age: 25]
print Object.has obj keyed 'name' ~ 输出 true
print Object.has obj keyed 'email' ~ 输出 false
Object.delete
删除对象的属性。以 keyed 指定键,返回布尔值。
const obj = *[name: 'Alice', age: 25]
Object.delete obj keyed 'age'
print obj ~ 输出 *[name: Alice]
Object.toPlain
转换为普通值。接收单个对象参数并返回其普通值表示。
const obj = *[name: 'Alice', age: 25]
print Object.toPlain obj ~ 输出 [object Object]
Object.toArray
转换为键值对数组。接收单个对象参数并返回数组。
const obj = *[name: 'Alice', age: 25]
print Object.toArray obj ~ 输出 [['name', 'Alice'], ['age', 25]]
打印对象
直接输出对象即显示其结构表示:
const obj = *[name: 'Alice', age: 25]
print obj ~ 输出 Object *[ String name: "Alice", Number age: 25 ]
属性访问
使用点语法访问属性:
const obj = *[name: 'Alice', age: 25]
print obj.name ~ 输出 'Alice'
print obj.age ~ 输出 25
使用点语法访问嵌套属性:
const obj = *[
user: *[
profile: *[
name: 'Alice'
]
]
]
print obj.user.profile.name ~ 输出 'Alice'
示例代码
~ 对象综合示例
~ 1. 创建对象
const person = *[name: 'Alice', age: 25]
print person
~ 2. 属性访问
print '姓名: ' .. person.name
print '年龄: ' .. person.age
~ 3. 检查属性
if Object.has person keyed 'name' {
print '对象包含 name 属性'
}
~ 4. 设置属性
Object.set person keyed 'city' to 'Beijing'
print person
~ 5. 获取属性(带默认值)
const email = Object.get person keyed 'email' or 'unknown'
print '邮箱: ' .. email
~ 6. 删除属性
Object.delete person keyed 'city'
print person
~ 7. 获取键、值、键值对
print ('所有键: ' .. (Object.keys person))
print ('所有值: ' .. (Object.values person))
print ('所有键值对: ' .. (Object.entries person))
~ 8. 对象克隆
const copy = Object.clone person
print copy
~ 9. 对象合并
const extra = *[job: 'Developer']
const merged = Object.contact person with extra
print merged
~ 10. 使用静态方法
const entries = [['a', 1], ['b', 2]]
const fromEntries = Object.fromEntries entries
print fromEntries
const source1 = *[x: 1]
const source2 = *[y: 2]
const source3 = *[z: 3]
const assigned = Object.assign source1 with source2 with source3
print assigned
~ 11. 嵌套对象
const nested = *[
user: *[
profile: *[
name: 'Bob',
avatar: 'avatar.png'
]
]
]
print '嵌套访问: ' .. (nested.user.profile.name)
~ 12. 对象遍历
const config = *[host: 'localhost', port: 8080, debug: true]
const keys = Object.keys config
for { let i = 0 } i < keys.length { i ++ } {
print (keys[i] .. ': ' .. (Object.get config keyed keys[i]))
}