javascript 在 node.js 中,为什么会有一个 util.isArray 和一个 Array.isArray?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22798826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In node.js, why is there a util.isArray and an Array.isArray?
提问by Evan Carroll
I just noticed the API docs for Node.js v0.10.26provide for util.isArray
,
我刚刚注意到Node.js v0.10.26的 API 文档提供了util.isArray
,
util.isArray(object)# Returns true if the given "object" is an Array.
false otherwise.
util.isArray(object)# 如果给定的“对象”是一个数组,则返回 true。
否则为假。
var util = require('util');
util.isArray([])
// true
util.isArray(new Array)
// true
util.isArray({})
// false
But, how is that different ecmascripts normal, Array.isArray?
但是,不同的 ecmascripts 是如何正常的,Array.isArray?
> Array.isArray([]);
true
> Array.isArray(new Array);
true
> Array.isArray({});
false
回答by josh3736
To actually answer whyutil.isArray
exists, we need a bit of a history lesson.
要真正回答为什么util.isArray
存在,我们需要上一堂历史课。
When it was first added to node, it did a bit more than call Array.isArray
.
当它第一次被添加到 node 时,它所做的不仅仅是调用Array.isArray
.
function isArray (ar) {
return ar instanceof Array
|| Array.isArray(ar)
|| (ar && ar !== Object.prototype && isArray(ar.__proto__));
}
This was a local function in utils and actually wasn't exporteduntil v0.6.0.
这是 utils 中的一个本地函数,实际上直到 v0.6.0才被导出。
In this form, util.isArray
handled a case that Array.isArray
doesn't:
在这种形式中,util.isArray
处理了一个Array.isArray
没有的案例:
> x = [1,2,3]
[ 1, 2, 3 ]
> y = Object.create(x)
[ , , ]
> Array.isArray(y)
false
> Array.isArray(Object.getPrototypeOf(y))
true
There's some discussion hereabout this behavior of util.isArray
, and consensus was that this behavior is actually bad because y
is not really an Array
.
这里有一些讨论一下这种行为util.isArray
,并一致认为这种行为实际上是不好的,因为y
不是一个真正的Array
。
Thus, the prototype-checking functionality was soon removed and replacedwith a check that used both Array.isArray
and a check of the argument's [[Class]].
因此,原型检查功能很快被删除,取而代之的是同时使用Array.isArray
的检查和对参数[[Class]] 的检查。
function isArray(ar) {
return Array.isArray(ar) ||
(typeof ar === 'object' && objectToString(ar) === '[object Array]');
}
However, checking the [[Class]]is actually duplicate effort because Array.isArray
also checksthe [[Class]], so it too was eventually removed– leaving only a call to Array.isArray
.
然而,检查[[类]实际上是重复的努力,因为Array.isArray
还检查了[[类],所以它也最终被删除-只留下一个电话Array.isArray
。
Today, util.isArray
is just an aliasof Array.isArray
.
今天,util.isArray
只是一个别名的Array.isArray
。
So in other words, the existence of util.isArray
is mostly a legacy thing and can be safely ignored.
所以换句话说, 的存在util.isArray
主要是遗留的东西,可以安全地忽略。
回答by SomeKittens
回答by Evan Carroll
@SomeKittenswas right, but that's a week ago. I patched node.js core and the docs. Now they're both the same thing, or will be in due time.
@SomeKittens是对的,但那是一周前。我修补了 node.js 核心和 docs。现在它们是同一回事,或者将在适当的时候出现。
> Array.isArray === util.isArray;
true
回答by alex
It's for consistency reasons.
这是出于一致性的原因。
node.js has util.isFunction()
, util.isObject()
, util.isArray()
and a number of similar functions. This way, type checks will all look similar to each other, instead of using different looking code each time.
的node.js有util.isFunction()
,util.isObject()
,util.isArray()
和一些类似的功能。这样,类型检查将看起来彼此相似,而不是每次都使用不同的代码。