JS中如何将类中对象的数字属性转换为数字并与其他数字相加?

发布于 2025-01-09 19:01:27 字数 641 浏览 0 评论 0原文

我想在客户类的方法中将两个值相加。其中一个是数字,另一个是存储在新类中的对象属性,发送方法(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜唯美灬不弃 2025-01-16 19:01:27

您可以使用一元加运算符进行转换使用前将金额改为数字

class Customer {
  constructor(name, password, balance) {
    {
      this.name = name;
      this.password = password;
      this.balance = balance;
    }

    this.send = function (amount) {
      return (this.balance -= +amount); // magic here
    };
    this.receive = function (amount) {
      return (this.balance += +amount); // and here
    };
  }
}

let student = new Customer("alex", "alex", 200);
let victim1 = new Customer("tom", "cat", 1000);

student.send(victim1.balance);
console.log(student.balance)
student.receive(victim1.balance);
console.log(student.balance)

student.send("500");
console.log(student.balance)
student.receive("100");
console.log(student.balance)

You can use unary plus operator to convert amount to number before use it

class Customer {
  constructor(name, password, balance) {
    {
      this.name = name;
      this.password = password;
      this.balance = balance;
    }

    this.send = function (amount) {
      return (this.balance -= +amount); // magic here
    };
    this.receive = function (amount) {
      return (this.balance += +amount); // and here
    };
  }
}

let student = new Customer("alex", "alex", 200);
let victim1 = new Customer("tom", "cat", 1000);

student.send(victim1.balance);
console.log(student.balance)
student.receive(victim1.balance);
console.log(student.balance)

student.send("500");
console.log(student.balance)
student.receive("100");
console.log(student.balance)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文