Published: 16 Feb 2018 › Updated: 16 Feb 2018

JavaScript Basics: Reflect.construct
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
Leave JavaScript Basics: Reflect.construct to:
Read more #javascript posts
Best Posts From ghasemkiani
We have not curated any of ghasemkiani's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From ghasemkiani
- Merging PDF Files with Java
- Happy Iranian New Year
- Stephen Hawking, one of the greatest physicists of our time, dies at 76
- Using Scoped NPM Packages
- Optimistic Typing in Nashorn
- JavaScript Basics: Reflect.apply
- JavaScript Basics: Reflect.construct
- JavaScript Basics: Reflect
- New Horizons Broke the Record of the “Pale Blue Dot” Picture
- JavaScript Basics: Object.freeze and Object.isFrozen
- JavaScript Basics: Object.seal and Object.isSealed
- JavaScript Basics: Object.preventExtensions and Object.isExtensible
- JavaScript Basics: Object.prototype.hasOwnProperty
- JavaScript Basics: Object.is
- JavaScript Basics: Object.keys, Object.values, and Object.entries
- What Is Success and How Do You Define It?
- Steemit Reached 700,000 Members
- JavaScript Basics: Object.getPrototypeOf and Object.setPrototypeOf
- JavaScript Basics: Object.assign
- JavaScript Basics: Object.create