ES6 class variable alternatives

前端 未结 15 2316
长发绾君心
长发绾君心 2020-11-22 06:04

Currently in ES5 many of us are using the following pattern in frameworks to create classes and class variables, which is comfy:



        
15条回答
  •  迷失自我
    2020-11-22 06:31

    Well, you can declare variables inside the Constructor.

    class Foo {
        constructor() {
            var name = "foo"
            this.method = function() {
                return name
            }
        }
    }
    
    var foo = new Foo()
    
    foo.method()
    

提交回复
热议问题