JS中 `=+` 是什么?

JS中 =+ 是什么?

依然是赋值

=是赋值,+代表后面的数字为正数,同理=-代表后面的数字为负数

用处

相当于告诉编译器,即将赋值的数值类型为数字类型,不要把数字当作字符串去拼接

示例

function Calculator(){
    this.read=function(){
    //此处不用=+的话,sum函数会返回数字拼接的字符串
      this.a=+prompt("a=",0);
      this.b=+prompt("b=",0);
    }
    this.sum=function(){
      return this.a+this.b;
    }
    this.multiply=function(){
      return this.a*this.b;
    }
  }

  let cal=new Calculator();
  cal.read();
  alert(cal.sum());
  alert(cal.multiply());
原文地址:https://www.cnblogs.com/sq800/p/14014954.html