javascript - JS forEach循环中的无符号右移赋值
问题描述
我找到了一个 javascript forEach 循环的实现,但有一件事让我很困扰。
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(fun /*, thisp*/) {
var len = this.length >>> 0;
if (typeof fun != "function") {
throw new TypeError();
}
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in this) {
fun.call(thisp, this[i], i, this);
}
}
};
}
无符号右移有什么意义this.length >>> 0
?
解决方案
这是一种确保它len
是 32 位无符号整数的方法。
通过使用>>> 0
,您永远无法获得NaN
或负值。
推荐阅读
- c# - “Xamarin.Android.Support.Core.UI”和“Xamarin.Android.Support.ViewPager”中存在“ViewPager”类型
- python - 编写一个函数来获取最后一次列 A/B 是特定值时列 C/D 的总和?
- git - Git致命错误:无法将代码拉到我的服务器
- python - 有没有办法让这个脚本只提供事件摘要唯一的事件摘要?
- c# - C# 或 VBA office word interop - 更改表格样式
- python - 多个盒子的盒子包装问题
- mysql - 如何将 jpg 类型作为原始类型的图像(blob 类型)从 mysql 检索到 matlab?
- python - 将多个 GluonTS 预测导出到 pandas 数据框
- javascript - 有人可以解释一下 javaScript 中的“空”语句和普通分号有什么区别吗
- ios - swift不能删除行