Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In node.js, why is there a util.isArray and an Array.isArray?

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 
like image 550
NO WAR WITH RUSSIA Avatar asked Apr 01 '14 23:04

NO WAR WITH RUSSIA


People also ask

What does Util do in node?

The util. debuglog() method is used to create a function that conditionally writes debug messages to stderr based on the existence of the NODE_DEBUG environment variable. If the section name appears within the value of that environment variable, then the returned function operates similar to console.

What is array isArray in JavaScript?

isArray() The Array. isArray() method determines whether the passed value is an Array .

What is an array In Node JS?

Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

What does node mean in JavaScript?

Node. js (Node) is an open source development platform for executing JavaScript code server-side. Node is useful for developing applications that require a persistent connection from the browser to the server and is often used for real-time applications such as chat, news feeds and web push notifications.


2 Answers

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.

like image 133
josh3736 Avatar answered Sep 29 '22 00:09

josh3736


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; 
like image 30
SomeKittens Avatar answered Sep 29 '22 00:09

SomeKittens