反射
正常情况下,需要通过一个构造函数,再通过 new 关键字创建实例化对象,从而调用该实例化对象的方法。而通过对象,来寻找其所在构造函数,或通过方法来找寻所在对象,这就是反射
静态方法
新的方法
| 方法名 | 描述 |
|---|---|
Reflect.Get (target, proKey[, thisTar]) |
获取target的proKey属性,若该属性设置了存取器,则thisTar可以设置该存储器中的this指向 |
Reflect.Set (target, proKey, value[, thisTar]) |
设置target的proKey属性为value,若该属性设置了存取器,则thisTar可以设置该存储器中的this指向 |
Reflect.Has (target, proKey) |
判断target对象中是否有proKey属性 |
Reflect.DeleteProperty (target, proKey) |
删除target对象中的proKey属性,成功返回true失败返回false |
Reflect.Construct (cons, args) |
使用cons构造函数,根据args参数创建实例对象,cons必须是构造函数,否则会报错 |
Reflect.Apply (func, thisTar, args) |
调用func函数,thisTar是函数中的this指向,args是函数中的参数的数组形式 |
Object 中迁移的方法
| 方法名 | 描述 | 对于 Object 对象中的方法 |
|---|---|---|
Reflect. GetPrototypeOf (obj) |
获取obj对象的prototype对象 |
Object.GetPrototypeOf () |
Reflect. SetPrototypeOf (obj, objPrototype) |
设置obj对象的prototyoe对象为objPrototype,不同的是该方法返回的是布尔值 |
Object.SetPrototypeOf () |
Reflect. DefineProperty (obj, propertyName, propertyDesc) |
通过描述对象,为obj对象定义某个属性 |
Object.DefineProperty () |
Reflect. GetOwnPropertyDescriptor (obj, propertyName) |
获取obj的propertyName属性的描述对象,没有该属性返回undefined |
Object.GetOwnPropertyDescriptor () |
Reflect. PreventExtensions (obj) |
防止obj对象扩展,无法添加新属性 |
Object.PreventExtensions () |
Reflect. IsExtensible (obj) |
判断obj对象是否可扩展 |
Object.IsExtensible () |
Reflect. OwnKeys (obj) |
返回obj对象自身的所有属性名的数组(包括不可枚举的属性和Symbols的键名) |
Object.GetOwnPropertyNames () Object. GetOwnPropertySymbols |

Comments NOTHING