为什么使用/ *,* / around参数以及为什么使用>>>提取数组的长度? [英] Why use /*, */ around arguments and why use >>> when extracting the length of an array?
问题描述
我在上查看indexOf页面上的javascript参考手册developer.mozilla.org 网站,并注意到indexOf的实现代码中的一些内容,我希望有人可以向我解释。
I was looking in the javascript reference manual on the indexOf page at developer.mozilla.org site, and noticed a few things in their implementation code of indexOf, I hope somebody can explain to me.
为了节省每个人往返于mozilla网站,这里是整个功能:
To save everybody a round trip to the mozilla site, here is the entire function:
if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length >>> 0;
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;
};
}
我不明白的是 函数声明中的/ *,来自* /
,零填充右移 >>>
提取数组的长度( var len = this.length>>> 0;
) 。
What I do not understand is the /*, from*/
in the function declaration, and the zero-fill right shift >>>
in the extracting of the length of the array (var len = this.length >>> 0;
).
推荐答案
来自* / 的 / *是一个注释掉的参数。但是,它似乎已留在评论中,表明此参数可以为该函数指定可选。
var from = Number(arguments[1]) || 0;
我相信参数[1]
会如果传入,则来自值的。
I believe that arguments[1]
would be the from
value if passed in.
arguments数组特别是
对函数有用,可以使用可变数量的
参数调用
,或者使用比
更多的参数,它们被正式声明接受。 http://www.devguru.com/Technologies/Ecmascript/Quickref/arguments。 HTML
>>>
是一个无符号右移。它在这里用于将潜在签名的数字长度
转换为无符号数字。
The >>>
is an unsigned right shift. It's being used here to convert a potentially signed number length
into an unsigned number.
http://www.c-point.com/javascript_tutorial/jsoprurshift.htm
这篇关于为什么使用/ *,* / around参数以及为什么使用>>>提取数组的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!