0
点赞
收藏
分享

微信扫一扫

Reflect


Reflect对象是—个全局的普通的对象。 Reflect的原型就是Object。Reflect是为操作对象而提供的新API。 

主要作用有:


  1. 将Object对象的属千语言内 部的方法放到Reflect对象上。
  2. 将用老 Object方法报错的情况,改为返回false。
  3. 让Object操作变成函数行为。另外, Reflect与Proxy是相辅相成的,在Proxy上 有的方法,在Reflect就—定有。


Reflect对象—共有13个静态方法。例如: 

Reflect.get(target, name, receiver)

//读取—个对象的属性 
let obj = {
name:"chen",
}

let result = Reflect.get(obj, "name")

console.log(result) //chen

Reflect.set(target, name ,value, receiver)

//设置该对象的属性借 
let obj = {
name: 'chen'
}

let result = Reflect.set(obj, "name", "shi")
console.log(result) //true
console.log(obj.name) //shi

Reflect. apply(target,thisArg,args)

//类似于Function.prototype.apply 
//继示目标对象的特定方法

Reflect.apply(Math.floor, undefined, [1.75]); //输出: 1;

Reflect.defineProperty(target, name ,desc)

//类似Object.defineProperty 
//返回借是—个Boolean的值
const obj = {};

const res = Reflect.defineProperty(obj,'b', {
configurable: true,
enumerable: true
})

console.log(res);//true


举报

相关推荐

0 条评论