Reflect 反射
Java 的反射机制是在编译阶段不知道是哪个类被加载,而是在运行的时候才加载、执行。
Reflect 是一个内置的对象,它提供拦截 JavaScript 操作的方法,这些方法与处理器对象的方法相同。Reflect不是一个函数对象,因此它是不可构造的。与大多数全局对象不同,Reflect没有构造函数。你不能将其与一个new运算符一起使用,或者将Reflect对象作为一个函数来调用。Reflect的所有属性和方法都是静态的(就像Math对象)
Reflect.apply()
通过指定的参数列表发起对目标(target)函数的调用。
语法
Reflect.apply(target, thisArgument, argumentsList)
参数 | 含义 | 必选 |
---|---|---|
target | 目标函数 | Y |
thisArgument | target函数调用时绑定的this对象 | N |
argumentsList | target函数调用时传入的实参列表,该参数应该是一个类数组的对象 | N |
// Reflect.apply():动态调用方法
console.log(Reflect.apply(Math.floor, null, [3.72])) //静态解析时不执行
let price = 110.1
// if (price > 100) {
// price = Math.floor.apply(null, [price])
// } else {
// price = Math.ceil.apply(null, [price])
// }
// console.log(price)
console.log(Reflect.apply(price > 100 ? Math.floor : Math.ceil, null, [price]))
Reflect.construct()
行为有点像 new操作符构造函数 , 相当于运行 new target(...args).
语法
Reflect.construct(target, argumentsList[, newTarget])
参数 | 含义 | 必选 |
---|---|---|
target | 被运行的目标函数 | Y |
argumentsList | 调用构造函数的数组或者伪数组 | Y |
newTarget | 该参数为构造函数, 参考 new.target 操作符,如果没有newTarget参数, 默认和target一样 | N |
// Reflect.construct():调用不同的类,动态实例化一个对象
let d = Reflect.construct(Date, [])
console.log(d.getTime(), d instanceof Date)
Reflect.defineProperty()
静态方法 Reflect.defineProperty() 基本等同于Object.defineProperty() 方法,唯一不同是返回 Boolean 值。
语法
Reflect.defineProperty(target, propertyKey, attributes)
参数 | 含义 | 必选 |
---|---|---|
target | 目标对象 | Y |
propertyKey | 要定义或修改的属性的名称 | Y |
attributes | 要定义或修改的属性的描述 | Y |
// Reflect.defineProperty 和 Object.defineProperty 唯一的区别是返回值不同
const student = {}
const r = Reflect.defineProperty(student, 'name', {
value: 'Mike'
})
const o = Object.defineProperty(student, 'name', {
value: 'Mike'
})
console.log(student, r) //{name: "Mike"} true
console.log(student, o) //{name: "Mike"} {name: "Mike"}
Reflect.deleteProperty()
允许你删除一个对象上的属性。返回一个 Boolean 值表示该属性是否被成功删除。它几乎与非严格的 delete operator 相同。
语法
Reflect.deleteProperty(target, propertyKey)
参数 | 含义 | 必选 |
---|---|---|
target | 删除属性的目标对象 | Y |
propertyKey | 将被删除的属性的名称 | Y |
const obj = {
x: 1,
y: 2
}
Reflect.deleteProperty(obj, 'x')
console.log(obj) // {y: 2}
Reflect.get()
Reflect.get() 方法的工作方式,就像从 object (target[propertyKey]) 中获取属性,但它是作为一个函数执行的。
语法
Reflect.get(target, propertyKey[, receiver])
参数 | 含义 | 必选 |
---|---|---|
target | 需要取值的目标对象 | Y |
propertyKey | 需要获取的值的键值 | Y |
receiver | 如果遇到 getter,此值将提供给目标调用 | N |
// Reflect.get():动态读取数据
const obj = {
x: 1,
y: 2
}
console.log(Reflect.get(obj, 'x')) // 1
console.log(Reflect.get([5, 6, 7], 1)) //6
Reflect.getOwnPropertyDescriptor()
静态方法 Reflect.getOwnPropertyDescriptor() 与 Object.getOwnPropertyDescriptor() 方法相似。如果在对象中存在,则返回给定的属性的属性描述符,否则返回 undefined。
语法
Reflect.getOwnPropertyDescriptor(target, propertyKey)
参数 | 含义 | 必选 |
---|---|---|
target | 需要寻找属性的目标对象 | Y |
propertyKey | 获取自己的属性描述符的属性的名称 | N |
const obj = {
x: 1,
y: 2
}
console.log(Reflect.getOwnPropertyDescriptor(obj, 'x'))
//{value: 1, writable: true, enumerable: true, configurable: true}
console.log(Object.getOwnPropertyDescriptor(obj, 'y'))
//{value: 2, writable: true, enumerable: true, configurable: true}
Reflect.getPrototypeOf()
静态方法 Reflect.getPrototypeOf() 与 Object.getPrototypeOf() 方法是一样的。都是返回指定对象的原型(即,内部的 [[Prototype]] 属性的值)。
语法
Reflect.getPrototypeOf(target)
解释
参数 | 含义 | 必选 |
---|---|---|
target | 获取原型的目标对象 | Y |
// Reflect.getPrototypeOf():查看实例对象上的原型链对象
let d = new Date()
console.log(Reflect.getPrototypeOf(d)) // 返回所有Date原型链上的方法
Reflect.has()
Reflect.has 用于检查一个对象是否拥有某个属性, 相当于in 操作符
语法
Reflect.has(target, propertyKey)
解释
参数 | 含义 | 必选 |
---|---|---|
target | 获取原型的目标对象 | Y |
propertyKey | 属性名,需要检查目标对象是否存在此属性 | Y |
// Reflect.has():查看对象上是否有某属性 注意-此方法 Object 对象上没有
const obj = {
x: 1,
u: 2
}
console.log(Object.has(obj, 'x')) // Uncaught TypeError: Object.has is not a function
console.log(Reflect.has(obj, 'x')) // true
console.log(Reflect.has(obj, 'z')) // false
Reflect.isExtensible()
Reflect.isExtensible 判断一个对象是否可扩展 (即是否能够添加新的属性),它与 Object.isExtensible() 方法一样。
语法
Reflect.isExtensible(target)
解释*
参数 | 含义 | 必选 |
---|---|---|
target | 获取原型的目标对象 | Y |
// Reflect.isExtensible():判断对象是否可扩展
const obj = {
x: 1,
u: 2
}
Object.freeze(obj) // 将obj冻结,使obj不可扩展
obj.z = 7
console.log(Reflect.isExtensible(obj)) // false
console.log(obj) // {x: 1, u: 2}
Reflect.ownKeys()
Reflect.ownKeys 方法返回一个由目标对象自身的属性键组成的数组。它的返回值等同于 Object.getOwnPropertyNames(target).concat(Object.getOwnPropertySymbols(target))
语法
Reflect.ownKeys(target)
解释*
参数 | 含义 | 必选 |
---|---|---|
target | 获取原型的目标对象 | Y |
// Reflect.isExtensible():判断对象是否可扩展
const obj = {
x: 1,
u: 2
}
//Reflect.ownKeys():获取对象自身属性
console.log(Reflect.ownKeys(obj)) // ["x", "u"]
console.log(Reflect.ownKeys([])) // ["length"]
console.log(Reflect.ownKeys([3, 4])) // ["0", "1", "length"]
Reflect.set()
Reflect.set 方法允许你在对象上设置属性。它的作用是给属性赋值并且就像 property accessor 语法一样,但是它是以函数的方式。
语法
Reflect.set(target, propertyKey, value[, receiver])
解释*
参数 | 含义 | 必选 |
---|---|---|
target | 获取原型的目标对象 | Y |
propertyKey | 设置的属性的名称 | Y |
value | 设置的值 | Y |
receiver | 如果遇到 setter,this 将提供给目标调用 | N |
// Reflect.set():修改或添加数据
const obj = {
x: 1,
u: 2
}
Reflect.set(obj, 'z', 4)
console.log(obj) //{x: 1, u: 2, z: 4}
const arr = ['a', 'b', 'c']
Reflect.set(arr, 2, 'g')
console.log(arr) //["a", "b", "g"]
Reflect.setPrototypeOf()
Reflect.setPrototypeOf 方法改变指定对象的原型 (即,内部的 [[Prototype]] 属性值)
语法
Reflect.setPrototypeOf(target, prototype)
解释*
参数 | 含义 | 必选 |
---|---|---|
target | 获取原型的目标对象 | Y |
prototype | 对象的新原型 (一个对象或 null) | Y |
// Reflect.setPrototypeOf():动态修改原型对象。示例:把数组的原型对象改成字符串的原型对象
console.log(Reflect.getPrototypeOf(arr)) // 获取数组的原型对象
Reflect.setPrototypeOf(arr, String.prototype) // 动态修改原型对象,把数组的原型对象改成字符串的原型对象
// arr.sort() // Uncaught TypeError: arr.sort is not a function
console.log(Reflect.getPrototypeOf(arr))
Reflect.preventExtensions()
Reflect.preventExtensions 方法阻止新属性添加到对象 例如:防止将来对对象的扩展被添加到对象中)。该方法与 Object.preventExtensions() 方法一致
语法
Reflect.preventExtensions(target)
解释*
参数 | 含义 | 必选 |
---|---|---|
target | 获取原型的目标对象 | Y |
示例
const obj = {
x: 1,
u: 2
}
// Reflect.preventExtensions():禁止对象扩展和Object.freeze()功能相同
console.log(Reflect.preventExtensions(obj))
console.log(Reflect.isExtensible(obj)) // false