<bdo id='cEKFF'></bdo><ul id='cEKFF'></ul>
  • <legend id='cEKFF'><style id='cEKFF'><dir id='cEKFF'><q id='cEKFF'></q></dir></style></legend>
    <i id='cEKFF'><tr id='cEKFF'><dt id='cEKFF'><q id='cEKFF'><span id='cEKFF'><b id='cEKFF'><form id='cEKFF'><ins id='cEKFF'></ins><ul id='cEKFF'></ul><sub id='cEKFF'></sub></form><legend id='cEKFF'></legend><bdo id='cEKFF'><pre id='cEKFF'><center id='cEKFF'></center></pre></bdo></b><th id='cEKFF'></th></span></q></dt></tr></i><div id='cEKFF'><tfoot id='cEKFF'></tfoot><dl id='cEKFF'><fieldset id='cEKFF'></fieldset></dl></div>

        <tfoot id='cEKFF'></tfoot>
      1. <small id='cEKFF'></small><noframes id='cEKFF'>

        为什么空数组类型转换为零?+[]

        时间:2023-08-07

          <bdo id='mTa0i'></bdo><ul id='mTa0i'></ul>
            <tbody id='mTa0i'></tbody>
            <i id='mTa0i'><tr id='mTa0i'><dt id='mTa0i'><q id='mTa0i'><span id='mTa0i'><b id='mTa0i'><form id='mTa0i'><ins id='mTa0i'></ins><ul id='mTa0i'></ul><sub id='mTa0i'></sub></form><legend id='mTa0i'></legend><bdo id='mTa0i'><pre id='mTa0i'><center id='mTa0i'></center></pre></bdo></b><th id='mTa0i'></th></span></q></dt></tr></i><div id='mTa0i'><tfoot id='mTa0i'></tfoot><dl id='mTa0i'><fieldset id='mTa0i'></fieldset></dl></div>
            1. <tfoot id='mTa0i'></tfoot>

                  <legend id='mTa0i'><style id='mTa0i'><dir id='mTa0i'><q id='mTa0i'></q></dir></style></legend>

                  <small id='mTa0i'></small><noframes id='mTa0i'>

                  本文介绍了为什么空数组类型转换为零?+[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  就在我以为我对 JavaScript 中的类型转换有所了解时,我偶然发现了这个:

                  just when I thought I understood something about type conversion in JavaScript, I stumbled with this:

                  +[]; // 0
                  Number([]); // 0
                  

                  我的第一个想法是我应该得到 NaN,就像我尝试将空对象转换为数字一样:

                  My first thought was that I should get NaN, just like if I try to convert an empty object to number:

                  +{}; // NaN
                  Number({}); // NaN
                  

                  我已经搜索了一段时间没有任何成功......

                  I have been searching about this for a while without any success...

                  有人能解释一下为什么它被转换为 0 而不是 NaN 吗?

                  Can somebody explain me why it gets converted to 0 and not to NaN?

                  这种行为标准吗?

                  谢谢.

                  推荐答案

                  简单来说有两个关键点:

                  In a brief, there are two key points:

                  • 空数组的toString方法返回一个空字符串.
                  • 使用 一元加运算符将空字符串 强制 归零 或作为函数调用的 Number 构造函数(例如 +"" === 0;).
                  • The toString method of an empty array returns an empty string.
                  • An empty string coerces to zero using the unary plus operator or the Number constructor called as a function (e.g. +"" === 0;).

                  例如,我们可以使用一个对象,它定义了一个toString方法,并返回一个空字符串得到一个等价的结果:

                  For example, we can use an object that defines a toString method, and returns an empty string to have an equivalent result:

                  var obj = { toString: function () { return "";} };
                  +obj; //  0
                  Number(obj); // 0
                  

                  这种行为是完全标准的.

                  This behavior is completely standard.

                  现在是长答案:

                  一元加运算符和Number 构造函数作为函数调用 在内部使用 ToNumber 抽象操作.

                  Both, the unary plus operator and the Number constructor called as a function internally use the ToNumber abstract operation.

                  ToNumber 将使用另外两个内部操作,ToPrimitive[[DefaultValue]].

                  ToNumber will use two more internal operations, ToPrimitive and [[DefaultValue]].

                  ToNumber 操作应用于对象时,例如您示例中的空数组,它调用 ToPrimitive 操作,以获取代表原始值并调用 ToNumber 再次使用该值 [1].

                  When the ToNumber operation is applied to an Object, such the empty array in your example, it calls the ToPrimitive operation, to get a representative primitive value and call ToNumber again using that value [1].

                  ToPrimitive 操作接收两个参数,一个 Value(这是您的数组对象)和一个提示类型,在本例中为Number",因为我们要进行数字转换.

                  The ToPrimitive operation receive two arguments, a Value (which is your array object), and a hint type, which in this case is "Number" since we want to make numeric conversion.

                  ToPrimitive 调用 [[DefaultValue]] 内部方法,同样带有数字"提示类型.

                  ToPrimitive calls the [[DefaultValue]] internal method, also with a "Number" hint type.

                  现在,由于我们使用数字"的提示类型,[[DefaultValue]] 内部方法现在将尝试首先调用对象上的 valueOf 方法.

                  Now, since the hint type we are using "Number", the [[DefaultValue]] internal method now will try to invoke first the valueOf method on the object.

                  数组对象没有特定的 valueOf 方法,该方法是继承自 Object.prototype.valueOf,并且这个方法简单地返回一个对object本身的引用.

                  Array objects don't have a specific valueOf method, the method is the one inherited from Object.prototype.valueOf, and this method simply returns a reference to the object itself.

                  由于 valueOf 方法没有产生 原始值,现在调用 toString 方法,它产生一个空字符串(这是一个原始值),然后 ToNumber 操作将尝试进行 String-Number 转换,最终以 0 [1].

                  Since the valueOf method didn't result in a primitive value, now the toString method is invoked, and it produces an empty string (which is a primitive value), then the ToNumber operation will try to do String-Number conversion and it finally end up with 0 [1].

                  但现在您可能想知道,为什么一个空字符串会强制为零?

                  But now you might wonder, why an empty string coerces to zero?

                  +""; // 0
                  

                  ToNumber内部操作应用于 String 类型,即 StringNumericLiteral 产生式.

                  There is a complete grammar that is used when the ToNumber internal operation is applied to a String type, the StringNumericLiteral production.

                  NumericLiteral 之间存在一些差异,其中一个差异是:

                  It has some differences between a NumericLiteral, and one of those differences is that:

                  为空或仅包含空格的 StringNumericLiteral 将转换为 +0.

                  A StringNumericLiteral that is empty or contains only white space is converted to +0.

                  这篇关于为什么空数组类型转换为零?+[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将布尔结果转换为数字/整数 下一篇:Javascript:避免字符串到整数或浮点类型转换与空或

                  相关文章

                  最新文章

                1. <i id='bMlYZ'><tr id='bMlYZ'><dt id='bMlYZ'><q id='bMlYZ'><span id='bMlYZ'><b id='bMlYZ'><form id='bMlYZ'><ins id='bMlYZ'></ins><ul id='bMlYZ'></ul><sub id='bMlYZ'></sub></form><legend id='bMlYZ'></legend><bdo id='bMlYZ'><pre id='bMlYZ'><center id='bMlYZ'></center></pre></bdo></b><th id='bMlYZ'></th></span></q></dt></tr></i><div id='bMlYZ'><tfoot id='bMlYZ'></tfoot><dl id='bMlYZ'><fieldset id='bMlYZ'></fieldset></dl></div>
                  <tfoot id='bMlYZ'></tfoot>

                    <legend id='bMlYZ'><style id='bMlYZ'><dir id='bMlYZ'><q id='bMlYZ'></q></dir></style></legend>
                      <bdo id='bMlYZ'></bdo><ul id='bMlYZ'></ul>

                    <small id='bMlYZ'></small><noframes id='bMlYZ'>