Javascript class variables

前端 未结 1 507
野性不改
野性不改 2021-01-27 07:10

I have encountered an error while trying to declare a variable inside of a Javascript class. Here is an example of code.

class BaseContainer {
  constructor(pare         


        
相关标签:
1条回答
  • 2021-01-27 07:44

    Well, you cannot declare variables inside a class. Put it in the constructor if you want to create a property. Also, you must not put semicolons after method declarations (including the constructor).

    class BaseContainer {
        constructor(parent) {
            this.someVar = 1;
            this.shell = document.createElement("div");
            parent.appendChild(this.shell);
        }
    }
    
    0 讨论(0)
提交回复
热议问题