Confusing JavaScript statement: “var x = new this();”

后端 未结 8 1182
野趣味
野趣味 2021-02-01 20:30

I thought I understood the concept of the JavaScript prototype object, as well as [[proto]] until I saw a few posts regarding class inheritance.

Firstly, \"JavaScript OO

8条回答
  •  感动是毒
    2021-02-01 21:00

    A simpler code explaination:

    class User {
      constructor() {
        this.name = '';
        this.age = '';
      }
      static getInfo() {
        let user = new this();
        console.log(user);
      } 
    }
    
    User.getInfo()
    

    Output:

    Object {
      age: "",
      name: ""
    }
    

提交回复
热议问题