最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - In node.js, why is there a util.isArray and an Array.isArray? - Stack Overflow

programmeradmin9浏览0评论

I just noticed the API docs for Node.js v0.10.26 provide for util.isArray,

util.isArray(object)# Returns true if the given "object" is an Array.

false otherwise.

var util = require('util');
util.isArray([])
  // true
util.isArray(new Array)
  // true
util.isArray({})
  // false

But, how is that different ecmascripts normal, Array.isArray?

> Array.isArray([]);
true
> Array.isArray(new Array);
true
> Array.isArray({});
false

I just noticed the API docs for Node.js v0.10.26 provide for util.isArray,

util.isArray(object)# Returns true if the given "object" is an Array.

false otherwise.

var util = require('util');
util.isArray([])
  // true
util.isArray(new Array)
  // true
util.isArray({})
  // false

But, how is that different ecmascripts normal, Array.isArray?

> Array.isArray([]);
true
> Array.isArray(new Array);
true
> Array.isArray({});
false
Share Improve this question edited Jan 26, 2015 at 1:41 Bergi 664k158 gold badges1k silver badges1.5k bronze badges asked Apr 1, 2014 at 23:27 Evan CarrollEvan Carroll 1
Add a comment  | 

4 Answers 4

Reset to default 69

To actually answer why util.isArray exists, we need a bit of a history lesson.

When it was first added to node, it did a bit more than call 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 exported until v0.6.0.

In this form, util.isArray handled a case that Array.isArray doesn't:

> 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 here about this behavior of util.isArray, and consensus was that this behavior is actually bad because y is not really an Array.

Thus, the prototype-checking functionality was soon removed and replaced with a check that used both Array.isArray and a check of the argument's [[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 checks the [[Class]], so it too was eventually removed – leaving only a call to Array.isArray.

Today, util.isArray is just an alias of Array.isArray.

So in other words, the existence of util.isArray is mostly a legacy thing and can be safely ignored.

While the Node.js source uses them both, util.isArray uses Array.isArray internally (source)

In util.js:

function isArray(ar) {
  return Array.isArray(ar);
}
exports.isArray = isArray;

@SomeKittens was 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.

> Array.isArray === util.isArray;
true

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.

发布评论

评论列表(0)

  1. 暂无评论