Say I have the following input (to be used with
Node
,but the problem is more general,and not Node related):
- Absolute path to a directory,call it
dirPathAbs
- An array of basenames (call it
namesSeq
) of some JS files that exist inside that folder
例如:
我可能有namesSeq = [‘a’,’b’,’c’],它对应于dirPathAbs中的一些a.js,b.js,c.js.
问题:
如何以纯粹的功能方式以及协变方式解析文件的路径? (即无需谈论迭代数组的变量.抱歉,协变量可能不是这个词).
我不想要的:
namesSeq.map(base => path.join(dirPathAbs,`${base}.js`));
也不
namesSeq.map(base => require.resolve(path.join(dirPathAbs,base)));
也不
namesSeq.map(base => path.resolve.bind(dirPathAbs)(base));
也不
const cb = base => path.resolve.bind(dirPathAbs)(base);
namesSeq.map(cb);
我期待这个工作
namesSeq.map(path.resolve.bind(dirPathAbs))
但事实并非如此.我认为path.resolve.bind(dirPathAbs)接收作为输入namesSeq,这是提供给Array.prototype.map的回调的第三个参数,因为我看到的错误是
TypeError: Path must be a string. Received [ 'a','b','c' ]
这只是让我感到沮丧的一种练习,但是自从学习JS以来,一整类类似的练习让我头疼.关于绑定方式以及所有这些Function.prototype,Array.prototype&朋友应该使用.
最佳答案