Reflect
Reflect.has
检查对象内有没有这个变量
Reflect.has(Object,"assign"); //true
"assign" in Object; //true
Reflect.get
获取对象内的指定变量
var obj={
name:"abc"
}
Reflect.get(obj,"name"); //true
Reflect.get(obj,"age"); //false
Reflect.set
设置对象内的指定变量
var obj={
name:"abc"
}
Reflect.set(obj,"name","jpc");
Reflect.set(obj,"age",19);
console.log(obj) //{ name:"jpc",age:19 }
Reflect.delete
删除对象内的指定变量
var obj={
name:"jpc",
area:"浙江"
}
Reflect.defineProperty(obj,"age",{
value:19,
writable:true,
configurable:true,
enumerable:false
});
Reflect.delete(obj,"name"); //true
Reflect.delete(obj,"age"); //false
Reflect.deltet(obj,"sex"); //true
console.log(obj); //{ area:"浙江" }
Reflect.constructor
不使用new方法构造一个实例
class Num{
constructor(...args){
for( let i of args ){
this[i]=i;
}
}
total(){
let totalNum=0;
for( let i in this ){
totalNum+=this[i];
};
return totalNum;
}
}
var n=Reflect.construct(Num,[5,8,9]);
var n2=new Num(5,8,9);
n.total()===n2.total() //true
Reflect.getPrototypeOf
获取对象的__proto__
的属性 =Object.getPrototypeOf(obj)
class Num{
constructor(...args){
for( let i of args ){
this[i]=i;
}
}
total(){
let totalNum=0;
for( let i in this ){
totalNum+=this[i];
};
return totalNum;
}
}
var n=Reflect.construct(Num,[5,8,9]);
var n2=new Num(5,8,9);
Reflect.getPrototypeOf(n)===Num.prototype //true
Reflect.getPrototypeOf(n2)===Num.prototype //true
Reflect.setPrototypeOf
设置对象的__proto__
属性 =Object.setPrototypeOf(obj, newProto)
class Num{
constructor(...args){
for( let i of args ){
this[i]=i;
}
}
total(){
let totalNum=0;
for( let i in this ){
totalNum+=this[i];
};
return totalNum;
}
}
var n=Reflect.construct(Num,[5,8,9]);
Reflect.setPrototypeOf(n,{name:"abc"});
n.__proto__ //{ name:"abc" }
Reflect.apply
function fun(...args){
console.log(this); //{namespace:"fun",time:"20200709"}
console.log(args); //[5,10]
}
Reflect.apply(fun,{namespace:"fun",time:"20200709"},[5,10]);
//=Function.prototype.apply.call(fun,{namespace:"fun",time:"20200709"},[5,10]),
Reflect.defineProperty
=Object.defineProperty
Reflect.ownKeys
返回Object.getOwnPropertyNames
与Object.getOwnPropertySymbols
之和
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。