TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript Reflect construct() Method

JavaScript Reflect construct() Method with examples on javascript reflect methods, apply() construct(), defineProperty() method, deleteProperty(), get(), getOwnPropertyDescriptor(), getPrototypeOf(), has(), isExtensible(), ownKeys(), setPrototypeOf() etc.

<< Back to JAVASCRIPT

JavaScript Reflect.construct() Method

The static Reflect.construct() method allows to invoke a constructor with a variable number of arguments. It gives also the added option to specify a different prototype.

Syntax

Reflect.construct(target, argumentsList[, newTarget])

Parameter

target: It is the target function to call.

argumentsList: It is an array-like object specifying the arguments with which target should be called.

newTarget: It is a constructor whose prototype should be used. See also the new.target operator. If newTarget is not present, it will treat as a target.

Return

This method returns a new instance of the target (or newTarget if present), initialized by the target as a constructor with the given arguments.

Exceptions

This exception will throw a TypeError if target or newTarget are not constructors.

Example 1

const a = new Array(1,2,3);
const b = Reflect.construct ( Array, [1,2,3] );
console.log(a);
console.log(b);

Output:

 [1, 2, 3]
 [1, 2, 3]

Example 2

function func1(a, b, c) {
  this.sum = a + b + c;
}
const args = [1, 2, 3];
const object1 = new func1(...args);
console.log(object1.sum);

Output:

 6

Example 3

function func1(a, b, c) {
  this.sum = a + b + c;
}
function func2(a, b, c) {
  this.sum = a + b + c;
}
const args2 = [1, 4, 3];
const args = [1, 2, 3];
const object1 = new func1(...args);
const object2 = Reflect.construct(func2, args2);
console.log(object2.sum);
console.log(object1.sum);

Output:

8
 6

Next TopicJavaScript Reflect




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf