Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

74
Views
How to dynamically import classes?

I've three js files file_a.js , file_b.js & file_c.js, they all have a class called Context:

export class Context{
constructor(){
...
}
Other_functions_which_are_different_for_each_file(){
...
}
}

Based on the argument passed in index.js, I'm calling the Context class from the relevant file.

index.js:

let call = document.currentScript.getAttribute("call");
let {Context} = import('./file'.concat(call));
...
async function app(){
camera = new Context();
}

The problem is index.js doesn't recognise Context as a class. So, I'm getting Context is not a constructor error on the line camera = new Context();.

How can I import the Context class, such that I'm not block scoped & can call the class inside the app() function?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Dynamic imports return a promise, so you will need to wait for that promise before you can use Context. For example:

const importPromise = import('./file'.concat(call));
...
async function app() {
  const { Context } = await importPromise;
  camera = new Context();
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!