es6 --- > Reflect的静态方法

Reflect.get(target, name, receiver): 查找并返回 target对象的 name属性,若没有,返回undefined

var myObject = {
    foo: 1,
    bar: 2,
    get baz() {
        return this.foo + this.bar;
    },
}

Reflect.get(myObject, 'foo');  // 1

// 若name属性部署了读取函数(getter),则读取函数的this 绑定receiver
var myObject = {
    foo: 1,
    bar: 2,
    get baz() {
        return this.foo + this.bar;
    },
};
var myReceiverObject = {
    foo: 4,
    bar: 4,
};
Reflect.get(myObject, 'baz', myReceiverObject); // 8

// 注:第一个参数要为对象

Reflect.set(target, name, value, receiver): 设置target对象的name属性等于value

var myObject = {
    foo: 1,
    set bar(value) {
        return this.foo = value;
    },
}

myObject.foo // 1
Reflect.set(myObject, 'foo', 2);
myObject.foo // 2
Reflect.set(myObject, 'bar', 3);
myObject.foo // 3

// 注:调用myObject对象的foo属性是直接复制,调用bar方法,将3传入,给foo赋值,

// Reflect.set 会触发Proxy.defineProperty拦截
let p = {
    a: 'a'
};
let handler = {
    set(target, key, value, receiver) {
        console.log('set');
        Reflect.set(target, key, value, receiver) 
    },
    defineProperty(target, key, attribute) {
        console.log('defineProperty');
        Reflect.defineProperty(target, key, attribute);
    }
};
let obj = new Proxy(p, handler);
obj.a = 'A';
// 当执行obj.a = 'A‘的时候,会执行handler.set方法,
// handler.set方法里面的Reflect.set会触发handler.defineProperty方法...

Reflect.has(obj, name): 对应name in obj 中的in 运算符

var myObject = {
    foo: 1,
};

// 旧写法
'foo' in myObject // true

// 新写法
Reflect.has(myObject, 'foo') // true

Reflect.deleteProperty(obj, name): 等同于delete obj[name], 用于删除对象的属性

const myObj = { foo: 'bar' };

// 旧写法
delete myObj.foo;

// 新写法
Reflect.delete(myObj, 'foo');

Reflect.construct(target, args): 等同于new target(…args)

function Greeting(name) {
    this.name = name;
}

// new 的写法
const instance = new Greeting('张三');

// Reflect.construct 的写法
const instance = Reflect.construct(Greeting, [' 张三']);
// 注意中括号,若没有会报错 Uncaught TypeError:CreateListFromArrayLike called on non-object

Reflect.getPrototypeOf(obj): 用于读取对象的__proto__属性,

const myObj = new FancyThing();

// 旧写法
Object.getPrototypeOf(myObj) === FancyThing.prototype;

// 新写法
Reflect.getPrototypeOf(myObj) === FancyThing.prototype;

// 注: Object.getPrototypeOf 中,若参数不是对象,会先将参数转换成对象,而Reflect会报错

Reflect.setPrototypeOf(obj,newProto): 用于设置对象的__proto__属性.

const myObj = new FancyThing();

// 旧写法
Object.setPrototypeOf(myObj, OtherThing.prototype);

// 新写法
Reflect.setPrototypeOf(myObj, OtherThing.prototype);

// 如果第一个参数不是对象,Object.setPrototypeOf 会返回第一个参数本身.
// Reflect.setPrototypeOf 会报错
// 如果第一个参数是 undefined 或null, Object 和 Reflect方法都会报错.
console.log(Object.setPrototypeOf(1, {}));
console.log(Reflect.setPrototypeOf(1,{}));
console.log(Object.setPrototypeOf(null,{}));
console.log(Reflect.setPrototypeOf(null,{}));

在这里插入图片描述
在这里插入图片描述
Reflect.apply(func, thisArg, args): 等同于Function.prototype.apply.call(func, thisArg, args)

const ages = [11, 33, 12, 54, 18, 96];

// 旧写法
const youngest = Math.min.apply(Math, ages);
const oldest = Math.max.apply(Math, ages);
const type = Object.prototype.toString.call(youngest);

// 新写法
const youngest = Reflect.apply(Math.min, Math, ages);
const oldest = Reflect.apply(Math.max, Math, ages);
const type = Refelct.apply(Object.prototype.toString, youngest, []);

Reflect.defineProperty(target, propertyKey, attributes): 等同于Object.defineProperty

function MyData() {
    /*...*/
}

// 旧写法
Object.defineProperty(MyDate, 'now', {
    value: () => Date.now()
});

// 新写法
Reflect.defineProperty(MyDate, 'now', {
    value: () => Date.now()
});

Reflect.getOwnPropertyDescriptor: 等同于Object.getOwnPropertyDescriptor,用于获取指定属性的描述对象

var myObject = {};
Object.defineProperty(myObject, 'hidden', {
    value: true,
    enumerable: false,
});

// 旧写法(第一个参数非对象时,返回undefined)
var theDescriptor = Object.getOwnPropertyDescriptor(myObject, 'hidden');

// 新写法(第一个参数非对象时,报错)
var theDescriptor = Reflect.getOwnPropertyDescriptor(myObject, 'hidden');

Reflect.isExtensible(target): 对应Object.isExtensible,表示对象是否可扩展

const myObject = {};

// 旧写法(若参数为非对象,会返回false)
Object.isExtensible(myObject);

// 新写法(若参数为非对象,会报错)
Reflect.isExtensible(myObject);

Reflect.preventExtensions(target): 对应Object.preventExtensions方法,用于将一个对象变为不可扩展

var myObject = {];

// 旧写法
Object.preventExtensions(myObject);

// 新写法
Reflect.preventExtensions(myObject);

// 若参入的参数是非对象
// ES5
Object.preventExtensions(1)  // 报错
// ES6
Object.preventExtensions(1)  // 1
// ES6
Reflect.preventExtensions(1) // 报错

Reflect.ownKeys(target): Object.getOwnPropertyNames 与 Object.getOwnPropertySymbols 之和

var myObject = {
    foo: 1,
    bar: 2,
    [symbol.for('baz')]: 3,
    [symbol.for('bing')]: 4,
};

// 旧写法
Object.getOwnPropertyNames(myObject)  // ['foo', 'bar']
Object.getOwnpropertySymbols(myObject) // [Symbol(baz), Symbol(bing)]

// 新写法
Reflect.ownKeys(myObject)   // ['foo', 'bar', Symbol(baz), Symbol(bing)]

参考《ES6标准入门》(第3版)P262~P270

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值