JavaScript Basics: Reflect.construct

in #javascript3 years ago (edited)

The method Reflect.construct helps construct a new instance from a constructor function given an array of arguments. It has a functionality similar to the new operator:

    function Employee (firstname, lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    }
    
    let employee1 = new Employee("John", "Doe");
    let employee2 = Reflect.construct(Employee, ["Jane", "Doe"]);
    
    // This can also be done with the spread operator:
    let args = ["John", "Smith"];
    let employee3 = new Employee(...args);

It is generally useful to have an alternative for the new operator, specially when we have an array of arguments. Even though this particular problem has been alleviated in ES6 with the introduction of the spread operator(...). (See the last line in the example code above.)


Related Posts

Sort:  

programing is a language of computer.i like this specialy,mathlab,fortan,c/c++.excelent
thanks for sharing

Good work.. You are writing nice and doing good for community.. I always told that computer is not my subject but always happy to see good work here.. And respect good teachers also

useful post on construct.

graet writing dear lovely friend @ghasemkiani. i all time like support and love your post and i well never forget you.

keep it up dear
@ghasemkiani

educative

Helpful post. thanks for the example.

بسیار عالی
بهترین لحظات را براتون آرزو میکنم

چه جالب
پس تعریف شی بصورت بالا انجام میشه در
reflect
ممنون

Wow amazing programming...thaNks for sharing...

Good information about reflect construct in javascript. You are guiding us like a teacher
Thank you very much @ghasemkiani

That's a great javascript...I like this post...

Informative content, thanks for sharing this post.

Thanks for sharing programming .
I appreciate this technology .
Best of luck.

good post, thanks for sharing....

This very nice post i appreciate your technology thanks for sharing ..Best of luck ...resteemit..

thank you D:@ghasemkiani
please come on my blog

you are really good at java programming

Informative content, thanks for sharing this post.

@ghasemkiani Helpful post. Good for beginners.

wow! amazing programming..thanks for [email protected] have a nice day

Resteemit done..Sir..

this is very informative blog .thanks to you for sharing..i praise
you..
Carry on

come on my blog

I like this post..
Resteemit done...

I like this programming..Keep it up..

will be using Reflect a lot more, thanks for the post

thanks for the post, do you have any idea about browser compatibility?

Thanks, I am learning javascript in codecademy but I am beginner

Coin Marketplace

STEEM 0.18
TRX 0.03
JST 0.028
BTC 36850.01
ETH 1245.87
USDT 1.00
SBD 3.21