【问题标题】:Question on Code in Mozilla's Array.prototype.indexOf [duplicate]Mozilla的Array.prototype.indexOf中的代码问题[重复]
【发布时间】:2010-12-01 06:46:04
【问题描述】:

这是 Mozilla 的 Array.prototype.indexOf 中的 Mozilla 代码

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt)
  {
    var len = this.length >>> 0; // What does ">>>" do?

    var from = Number(arguments[1]) || 0;
    from = (from < 0) ? Math.ceil(from): Math.floor(from); 
    if (from < 0)from += len;

    for (; from < len; from++)
    {
      if (from in this && this[from] === elt)return from;
    }
    return -1;
  };
}

我不懂一些语法。
上面代码中的“>>>”是做什么的?

【问题讨论】:

标签: javascript


【解决方案1】:

这是一个无符号右移——它们基本上是作为一种非常快速的方式来转换为有效的数组索引。

【讨论】:

    【解决方案2】:

    我认为这是一个无符号右移运算符

    【解决方案3】:

    这是一个无符号右移,正如这里指出的:http://www.c-point.com/javascript_tutorial/jsoprurshift.htm,但它应该移动第二个数字中的位数(在 >>> 的右侧)。

    【讨论】:

      【解决方案4】:

      另见Why use /*, */ around arguments and why use >>> when extracting the length of an array?

      “>>> 是一个无符号右移。它在这里被用来将一个潜在的有符号数长度转换为一个无符号数。”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-26
        • 1970-01-01
        • 2020-06-05
        • 2020-07-28
        • 1970-01-01
        • 1970-01-01
        • 2016-06-08
        相关资源
        最近更新 更多