Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Replace constructor === Array checks with safer Array.isArray #179

wants to merge 1 commit into


None yet
5 participants

MDN Object constructor reference page ( https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/constructor ) states that constructor property can be changed and it is not always safe to believe in constructor function.

I found an issue when using the latest version of async with the latest version of sandboxed-module ( https://github.com/felixge/node-sandboxed-module ) where async.parallel thinks that tasks variable is not an array even though it is actually an array. This is caused by "tasks.constructor === Array" returns false, while Array.isArray(tasks) returns true.
This is very likely to be caused by sandboxed-module issue 13 ( felixge/node-sandboxed-module#13 ) which changes the value of constructor property, and is still an open issue.

Granted that this is not an async bug, but it would be better if async doesn't rely on constructor property which can be modified by other modules, and uses Array.isArray instead.


caolan commented Jan 31, 2013

Please bear in mind that async is also a browser library and will need a browser-compatible fallback for this.

qubyte commented Feb 6, 2013

MDN suggests using the trick

Object.prototype.toString.call(array) === '[object Array]';

A function can be aliased to Array.isArray if available, and otherwise this. This solution is guaranteed to work by the 5th edition standard. However, various versions of IE fall down when an array comes from a different frame, in which case the test will return false because you get '[object Object]'. If arrays from other frames with IE is not a great concern, then the above may be an acceptable shim.


caolan commented Feb 6, 2013

@qubyte that's the trick I'd normally use for shimming Array.isArray, so it's acceptable to me at least ;)

Don't forget about the various !== Array tests!


dougwilson commented Mar 20, 2013

Also, @qubyte's comments regarding Arrays from other frames would have already not worked with the constructor tests, as Array in one frame would not actually be the same Array as another frame, so I'd say it's a non-issue regarding replacing obj.constructor === Array constructs.

Closing this issue since #303 has a more comprehensive fix.

@cliffano cliffano closed this Jul 29, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment