Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can we generalize the constructor with the variable formatting in Javascript?

Suppose,

My Code:

var slist = [‘ec’, ‘ce’, ‘ke’]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for(let s in slist){
const s1 = slist[s]

//here, AWS is class Name and it has methods which are in slist.

const ec2 = new AWS.s1()
}

Requirement: Now, when creating the constructor with the methods of AWS class, I want that s1 variable which is having method name should create a constructor.

>Solution :

If AWS.ec, AWS.ke, AWS.ce are the properties used when hard-coded, you can access then via a variable using square-bracket notation

const slist = ['ec', 'ce', 'ke']

slist.forEach(s => {
  const ec2 = new AWS[s]()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading