我正在查看Mozilla的代码,它为Array添加了一个过滤方法,它有一行代码让我很困惑.
var len = this.length >>> 0;
Run Code Online (Sandbox Code Playgroud)
我以前从未见过用于JavaScript的>>>.
它是什么,它做了什么?
在这样的代码行中找到了这个运算符:
var t = Object(this),
len = t.length >>> 0;
Run Code Online (Sandbox Code Playgroud)
这个算子是什么意思?
完整代码如下.它是JS some方法的代码:
if (!Array.prototype.some) {
Array.prototype.some = function(fun /*, thisp */) {
"use strict";
if (this == null) throw new TypeError();
var t = Object(this),
len = t.length >>> 0;
if (typeof fun != "function") throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in t && fun.call(thisp, t[i], i, t))
return true;
} …Run Code Online (Sandbox Code Playgroud) 我刚看到这段代码:
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (elt /*, from*/) {
var len = this.length >>> 0; // 3rd line
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;
};
}
Run Code Online (Sandbox Code Playgroud)
什么是>>>做3号线?