unexpected token = for class properties in node 8.4

前端 未结 2 1326
天命终不由人
天命终不由人 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:36

    Instance class fields will be supported as of Node v12, so one solution is to use a version >= 12 once it is released.

    https://node.green/#ESNEXT-candidate--stage-3--instance-class-fields

    For now if you're interested the nightly builds are at: https://nodejs.org/download/nightly/

    0 讨论(0)
  • 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 = [];
    
    0 讨论(0)
提交回复
热议问题