V
V
Vyacheslav Shevchenko2019-10-22 12:14:29
JavaScript
Vyacheslav Shevchenko, 2019-10-22 12:14:29

How to instantiate a JS class in a document?

Hello.
I use:

"@babel/preset-env": "^7.6.3",
    "babel-loader": "^8.0.6",
    "webpack": "^4.41.2",
    "webpack-cli": "^3.3.9"

There is a class file:
class MyClass {
   constructor(){ this.init()}
   init () {}
}

How can I create an instance of this class in an html document?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Belyaev, 2019-10-22
@WebDev2030

Webpack creates an isolated scope for each module. You need to manually put your class into the global object:

class MyClass {
   constructor(){ this.init()}
   init () {}
}

// например вот так:
window.MyClass = MyClass;

A
Alex, 2019-10-22
@Kozack

Most likely your class and instantiation are in different scopes.
Because the syntax is correct
5daed8dfa366f862171706.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question