node.js - Array.forEach / .map returning error when used with path.resolve -
i'm using nodejs 0.10.13. i'm curious behavior of following code snippet:
> var = ['1','2','3'] undefined > a.map(function(){return path.resolve(arguments[0])}) [ '/users/user/1', '/users/user/2', '/users/user/3' ] > a.map(path.resolve) typeerror: arguments path.resolve must strings @ exports.resolve (path.js:313:15) > a.map(path.resolve.bind(path))) typeerror: arguments path.resolve must strings @ exports.resolve (path.js:313:15)
why 2nd , 3rd map
calls return error when array has strings? going relevant line in nodejs's source code yields this:
if (typeof path !== 'string') { throw new typeerror('arguments path.resolve must strings'); } else if (!path) { continue; }
which makes no sense why arguments not strings. have clue?
this happens because parameters passed each call of mapped function not actual elment, array index , whole array.
to see parameters gets sent map
, try this:
> var = ['1', '2', '3']; ['1', '2', '3'] > a.map(function() { return arguments}); [ { '0': '1', '1': 0, '2': [ '1', '2', '3' ] }, { '0': '2', '1': 1, '2': [ '1', '2', '3' ] }, { '0': '3', '1': 2, '2': [ '1', '2', '3' ] } ]
since object sent mapped function (path.resolve
in case) not string object, typeerror
.
Comments
Post a Comment