- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Call a JavaScript class object with variable?
Let’s say the following is our variable −
var message = 'This is the Class Demo';
Following is our object,
var object = new FirstClass(message)
The class FirstClass −
class FirstClass{ constructor( message){ this.message = message; } }
We will use eval() to call a JavaScript class object with variable. Following is the code −
Example
class FirstClass{ constructor( message){ this.message = message; } } var message = 'This is the Class Demo'; var object = new FirstClass(message) console.log(eval(object).message);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo121.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo121.js This is the Class Demo
Advertisements
To Continue Learning Please Login
Login with Google