JavaScript Basics: Reflect.construct

in javascript •  7 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

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

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 shairing@ghasemkiani. have a nice day

Resteemit done..Sir..

  ·  7 years ago Reveal Comment

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