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();
Run Code Online (Sandbox Code Playgroud)
调用它时,控制台只显示“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();
Run Code Online (Sandbox Code Playgroud)
如需解释,请查看评论或此链接
另一个值得阅读的参考:https : //stackoverflow.com/a/37004175/10473393(感谢@Amy 在评论中分享)
| 归档时间: |
|
| 查看次数: |
116 次 |
| 最近记录: |