Custom printing from console.log?

后端 未结 3 1220
庸人自扰
庸人自扰 2021-01-21 12:22

Let\'s say I have this class.

class Attribute {
  constructor(name) {
    this.name = name;
  }
}

And I create an instance and log it to the c

3条回答
  •  情歌与酒
    2021-01-21 13:13

    class Attribute {
      constructor(name) {
        this.name = name;
      }
      toString(){//simply set the to String method?
        return "{Attribute} "+this.name;
      }
    }
    

    As console.log does not call to String you either do:

    const test = new Attribute('Large');
    console.log(test+"");
    

    or you create your own logging function

提交回复
热议问题