# 六.对象

# 1.对象

//Object.assign 合并对象

let name = { name: "zbc" }
let age = { age: 9 }
let obj = Object.assign(name, age) //浅拷贝
console.log(obj)

//__proto__ 链
console.log("1".__proto__)

let name = { name: "zbc" }
let age = { age: 9 }
name.__proto__ = age
console.log(name.age)

//设置
Object.setPrototypeOf(name, age)
//获取
Object.getPrototypeOf(name)

let age = {
  age: 8,
  name: 123,
}
let obj = {
  name: "zbc",
  getName() {
    //可以通过super关键字获取父属性
    return super.name
  },
  __proto__: age,
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# 2. is

# 3. assign

# 4. setPrototypeOf

# 5. proto

# 6. super

# 2.proto

console.log("1".__proto__)
1

以前设置原型链

let name = { name: "zbc" }
let age = { age: 9 }
name.__proto__ = age
console.log(name.age)
1
2
3
4

现在设置原型链

Object.setPrototypeOf(name, age)
1

获取原型链

Object.getPrototypeOf(name)
1

可以通过 super 关键字获取父属性

let age = {
  age: 8,
  name: 123,
}
let obj = {
  name: "zbc",
  getName() {
    return super.name
  },
  __proto__: age,
}
console.log(obj.getName())
1
2
3
4
5
6
7
8
9
10
11
12