- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
- Related Articles
- Call a function with onclick() – JavaScript?
- JavaScript - Set object key by variable
- How to call the constructor of a parent class in JavaScript?
- Can super class reference variable hold sub class's object in java?
- JavaScript call() Method with Arguments.
- How to access a derived class member variable by an interface object in Java?
- How to create an object property from a variable value in JavaScript?
- How to call jQuery function with JavaScript function?
- Invoking functions with call() and apply() in JavaScript
- How to call a function from a string stored in a variable PHP?
- How to call a parent class function from derived class function in C++?
- How to call a method of a class in C#
- JavaScript Function Call
- How to access an object value using variable key in JavaScript?
- What is a JavaScript call() Method?

Advertisements