首页 > 解决方案 > Why does console.log exclude my zero from logging?

问题描述

class Person {
  constructor(name, address, phone) {
    this.name = name;
    this.address = address;
    this.phone = phone;
  }

  call() {
    console.log(`Calling ${this.phone}...`)
  }
}

const tom = new Person('Tom', 'Uden', 0619616);
tom.call();

When calling this, the console only shows "619616". When I add console.log(tom.phone) it does log the entire number. Why is the zero excluded when calling a method?

标签: javascript

解决方案


您将其用作整数。

如果要保留零,请将其用作字符串。

class Person {
  constructor(name, address, phone) {
    this.name = name;
    this.address = address;
    this.phone = phone;
  }

  call() {
    console.log(`Calling ${this.phone}...`)
  }
}

const tom = new Person('Tom', 'Uden', '0619616');
tom.call();

如需解释,请查看评论或此链接

另一个值得阅读的参考:https : //stackoverflow.com/a/37004175/10473393(感谢@Amy 分享评论)


推荐阅读