unexpected token = for class properties in node 8.4

前端 未结 2 1325
天命终不由人
天命终不由人 2021-01-17 11:12

running the following code in node (v8.4)

class TodoStore {
    todos = [];

    get completedTodosCount() {
        return this.todos.filter(
            to         


        
2条回答
  •  一向
    一向 (楼主)
    2021-01-17 11:39

    Update
    Support for instance class fields starts with node >= 12.


    Literal class properties are not supported by any version of node, according to this table. You'll still have to set any instance properties inside your class constructor:

    class TodoStore {
    
        constructor() {
            this.todos = [];
        }
        // ...
    }
    

    If you wish to define a static property, you'd assign that directly to the TodoStore reference, after the class has been declared:

    TodoStore.todos = [];
    

提交回复
热议问题