小编KKB*_*KKB的帖子

object destructing inside constructor in nodejs to assign values to member variable not working

I am trying ES6 object destructuring inside a constructor hoping to assign value to a member variable. It is not working. Its showing undefined when I am printing the value inside a member function. Its printing correctly if I print inside the constructor.Is this valid?

    class Environment {
        constructor(env) {
            const { name, version } = env
            console.log(name)
        }

        printName() {
            console.log(this.name)
        }
    }
    var obj = { "name": "inst1", "version": "12.2.7" };
    var envObj = new Environment(obj);
    envObj.printName();
Run Code Online (Sandbox Code Playgroud)

node.js ecmascript-6 object-destructuring

4
推荐指数
1
解决办法
1627
查看次数