gpt4 book ai didi

javascript - 为什么 console.log 从日志记录中排除我的零?

转载 作者:行者123 更新时间:2023-11-30 07:12:02 27 4
gpt4 key购买 nike

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();

调用时,控制台只显示“619616”。当我添加 console.log(tom.phone) 时,它会记录整个号码。为什么调用方法时排除零?

最佳答案

您将其用作整数。

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

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();

有关解释,请查看评论或 this link

另一个值得一读的引用资料:https://stackoverflow.com/a/37004175/10473393 (感谢@Amy 在评论中的分享)

关于javascript - 为什么 console.log 从日志记录中排除我的零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59328162/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com