Skip to content

Commit 36eaf3f

Browse files
committed
增加至Reflect
1 parent 631b96f commit 36eaf3f

1 file changed

Lines changed: 82 additions & 1 deletion

File tree

JavaScriptES6-10_notes/JavaScriptES6-10.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,19 +1124,100 @@ Promise.race([p1(), p2()]).then((value) => {
11241124

11251125
- Java 的反射机制是在编译阶段不知道是哪个类被加载 ,而是在运行的时候加载、执行
11261126

1127+
```js
1128+
console.log(Math.floor.apply(null, [3.111]))
1129+
1130+
console.log(Reflect.apply(Math.floor, null, [2.33]))
1131+
1132+
let price = 90.12
1133+
console.log(Reflect.apply(price > 100 ? Math.floor : Math.ceil, null, [price]))
1134+
```
1135+
1136+
1137+
11271138
### 2-45 Reflect.construct(反射机制)
11281139

1140+
- 动态实例化一个类
1141+
1142+
```js
1143+
let d = Reflect.construct(Date, [])
1144+
console.log(d.getTime(), d instanceof Date)
1145+
```
1146+
1147+
- `Reflect.defineProperty` 新增对象属性
1148+
- 与 Object 区别在于返回值不同,Reflect 返回true/false,Object 返回对象
1149+
- 所有具备反射机制的功能性函数都会放到Reflect 上面去
1150+
- 将来Object 中可能会移除这些方法
1151+
- `Reflect.get` 读取数据操作
1152+
1153+
```JS
1154+
const student = {}
1155+
const r2 = Object.defineProperty(student, 'name', { value: 'Mike' })
1156+
// console.log(student)
1157+
const r = Reflect.defineProperty(student, 'name', { value: 'Mike2' })
1158+
console.log(student, r, r2)
1159+
1160+
const obj = { x: 1, y: 2 }
1161+
// Reflect.deleteProperty(obj, 'x')
1162+
console.log(obj)
1163+
console.log(Reflect.get(obj, 'y'))
1164+
console.log(Reflect.get([3, 4], 1))
1165+
```
1166+
11291167

11301168

11311169
### 2-46 Reflect.getOwnPropertyDescriptor(反射机制)
11321170

1171+
- 获取属性描述符
1172+
- `Reflect.getPrototypeOf(obj)`**获取实例对象的原型对象**
1173+
- `Reflect.has(obj, 'property')`:验证对象下面有没有这个属性,Object 没有这个方法
1174+
- `Reflect.isExtensible(obj)`:是否可扩展,是否被冻结了
1175+
- `Object.freeze(obj)`:冻结对象,不可扩展
1176+
- `Reflect.ownKeys()`:返回对象的键
1177+
- `Reflect.preventExtensions(obj) `:效果和 freeze 相同
1178+
- `Reflect.set(obj,'z','1')` :设置数据
1179+
- `Reflect.setPrototypeOf(obj,'type')`**设置实例对象的原型对象**
1180+
1181+
```js
1182+
const obj = { x: 1, y: 2 }
1183+
// console.log(Reflect.getOwnPropertyDescriptor(obj, 'x'))
1184+
// console.log(Object.getOwnPropertyDescriptor(obj, 'y'))
1185+
1186+
// let d = new Date()
1187+
// console.log(Reflect.getPrototypeOf(d))
1188+
// console.log(Object.getPrototypeOf(obj))
1189+
1190+
// console.log(Reflect.has(obj, 'y'))
1191+
// Object.freeze(obj)
1192+
// obj.z = 3
1193+
1194+
// console.log(Reflect.isExtensible(obj), obj)
1195+
// console.log(Reflect.ownKeys(obj))
1196+
// console.log(Reflect.ownKeys([1, 2, 3]))
1197+
// Symbol
1198+
1199+
// Reflect.preventExtensions(obj) // 效果和 freeze 相同
1200+
// console.log(Reflect.isExtensible(obj))
1201+
1202+
Reflect.set(obj, 'z', 1)
1203+
console.log(obj)
1204+
const arr = ['a', 'w', 's']
1205+
Reflect.set(arr, 3, 'oioo')
1206+
console.log(arr)
1207+
1208+
console.log(Reflect.getPrototypeOf(arr))
1209+
Reflect.setPrototypeOf(arr, String.prototype)
1210+
console.log(Reflect.getPrototypeOf(arr))
1211+
1212+
```
1213+
11331214

11341215

11351216
## Proxy 代理
11361217

11371218
### 2-48 proxy basic syntax(该怎样使用代理功能)
11381219

1139-
1220+
-
11401221

11411222
### 2-49 Schema Validation(1)
11421223

0 commit comments

Comments
 (0)