JavaScript类变量 [英] Javascript class variables

查看:45
本文介绍了JavaScript类变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试在Javascript类中声明变量时遇到错误。这是代码示例。

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(parent){
    this.Shell = document.createElement("DIV");
    parent.appendChild(this.Shell);
  };
  this.SomeVar = 1;
};

这给了我一个错误。

推荐答案

那么,您不能在 class 内声明变量。如果要创建属性,请将其放在构造函数中。另外,您不得在方法声明之后加上分号(包括构造函数)。

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);
    }
}

这篇关于JavaScript类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆