JavaScript Basics: Reflect.construct

in javascript •  10 months ago

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.

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

Helpful post. thanks for the example.

educative

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

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

good post, thanks for sharing....

Resteemit done..Sir..

come on my blog

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

I like this programming..Keep it up..

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

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

you are really good at java programming

Informative content, thanks for sharing this post.

@ghasemkiani Helpful post. Good for beginners.

thank you D:@ghasemkiani
please come on my blog

Informative content, thanks for sharing this post.

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

I like this post..
Resteemit done...

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

wow! amazing programming..thanks for shairing@ghasemkiani. have a nice day

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