JavaScript中的= + _是什么意思 [英] What does = +_ mean in JavaScript

查看:180
本文介绍了JavaScript中的= + _是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道JavaScript中的 = + _ 运算符是什么意思。它看起来像是分配。

I was wondering what the = +_ operator means in JavaScript. It looks like it does assignments.

示例:

hexbin.radius = function(_) {
   if (!arguments.length)
       return r;
   r = +_;
   dx = r * 2 * Math.sin(Math.PI / 3);
   dy = r * 1.5;
   return hexbin;
};


推荐答案

r = +_;




  • + 尝试将任何 _ 投射到一个数字。

  • _ 仅限于变量名(不是运算符),可以是 a foo 等。

    • + tries to cast whatever _ is to a number.
    • _ is only a variable name (not an operator), it could be a, foo etc.
    • 示例:

      +"1"
      

      将1投射到纯数字1。

      var _ = "1";
      var r = +_;
      

      r 现在 1 ,而不是1

      此外,根据算术运算符上的MDN页面


      一元加号运算符在其操作数之前,并计算为其
      操作数,但尝试将其转换为数字,如果它不是
      已经
      [...] 它可以转换整数和
      浮点数的字符串表示形式,以及非字符串值 true false null
      十进制和十六进制(0x -prefixed)格式的整数支持
      。支持负数(但不支持十六进制)。如果
      无法解析特定值,它将评估为 NaN

      The unary plus operator precedes its operand and evaluates to its operand but attempts to converts it into a number, if it isn't already. [...] It can convert string representations of integers and floats, as well as the non-string values true, false, and null. Integers in both decimal and hexadecimal ("0x"-prefixed) formats are supported. Negative numbers are supported (though not for hex). If it cannot parse a particular value, it will evaluate to NaN.

      还注意到


      一元加号是将某些东西转换为数字的最快和首选方式

      unary plus is the fastest and preferred way of converting something into a number

      这篇关于JavaScript中的= + _是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆