Reflect personal records

Reflect is a built-in method, and can not be constructed constructor

Reflect.construct:

And new operating parent has some similar, equivalent to new target (...)

Example:

We define a class of their own

Box {class 
constructor (Arg ...) {
Object.assign (the this, {A:. 1, ARR: [Arg ...]})
}
Play () {
the console.log ( "Play")
}
}
then new operation to create an instance of the parent
let  box=new Box()
console.log(box)
box.play()

 Then create an instance by Reflect.construct

let boxs=Reflect.construct(Box,[])
console.log(boxs)
boxs.play()

 

 

 Both methods can see basically no difference, created out by the method of Reflect instance constructor you can call out. But note: Use Reflect.construct must pass an array as a parameter. Objects can also theoretically, but in the constructor and can be used inside the object.

Reflect.construct create an object can also be achieved, this point and there are some similar Object.create

function one() {
this.a=1
}
function two() {
this.a=2
}
let obj=Object.create(one.prototype)
one.apply(obj)
console.log(obj.a) //1

let objs = Reflect.construct (one, [ ]) one in this function will automatically bind to the OBJS 
the console.log (objs.a). 1 //

Guess you like

Origin www.cnblogs.com/ayujun/p/11526933.html