Loading

JS中 `=+` 是什么?

JS中 =+ 是什么?

看别人代码,出现了=+这样的写法,非常迷惑。

依然是赋值

=是赋值,+代表后面的数字为正数,同理=-代表后面的数字为负数
(注意代码格式啊,多加空格!!可以使用格式化插件prettier一键格式化)

用途

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

示例

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());
posted @ 2020-11-21 11:13  sq800  阅读(1561)  评论(0编辑  收藏  举报