JS中如何将类中对象的数字属性转换为数字并与其他数字相加?
我想在客户类的方法中将两个值相加。其中一个是数字,另一个是存储在新类中的对象属性,发送方法(ab)工作良好,但接收方法(a+b)不能作为数字工作,因为它是一个对象并且它就像js中的字符串,有什么解决办法吗?
export class Customer {
constructor(name, password, balance) {
{
this.name = name;
this.password = password;
this.balance = balance;
}
this.send = function (amount) {
return (this.balance -= amount);
};
this.receive = function (amount) {
return (this.balance += amount);
};
}
}
export let student = new Customer("alex", "alex", 200);
export let victim1 = new Customer("tom", "cat", 1000);
export let victim2 = new Customer("jerry", "mous", 500);
I want to add 2 values together at the method in the customer class. one of them is a number and the other is an object property stored in a new class, the send method(a-b) works well, but the receive method(a+b) doesn't work as a number because it's an object and it is like a string in js, what is the solution?
export class Customer {
constructor(name, password, balance) {
{
this.name = name;
this.password = password;
this.balance = balance;
}
this.send = function (amount) {
return (this.balance -= amount);
};
this.receive = function (amount) {
return (this.balance += amount);
};
}
}
export let student = new Customer("alex", "alex", 200);
export let victim1 = new Customer("tom", "cat", 1000);
export let victim2 = new Customer("jerry", "mous", 500);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用一元加运算符进行转换使用前将金额改为数字
You can use unary plus operator to convert amount to number before use it