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