【发布时间】:2015-07-21 20:36:07
【问题描述】:
出于好奇:
MDN 教我如何使用这样的快捷功能应用:
trim = Function.prototype.call.bind(String.prototype.trim)
join = Function.prototype.call.bind(Array.prototype.join)
现在,我可以映射trim,但由于某种原因不能映射join。 join 将 ',' 作为默认参数(分隔符),所以我应该没问题,但它使用数组索引:
> trim = Function.prototype.call.bind(String.prototype.trim)
call()
> [' a','b '].map(trim)
["a", "b"]
> join = Function.prototype.call.bind(Array.prototype.join)
call()
> [['a','b'],['c','d']].map(join)
["a0b", "c1d"]
为什么?
另外,如果我真的想要一个不同的分隔符怎么办?将它传递给bind 不起作用,因为它被附加到现有参数(在任何时候我映射到的列表的元素之一)之前。然后它扮演要连接的字符串的角色,如果有要分隔的东西,要连接的字符串将充当分隔符:
> joins = Function.prototype.call.bind(Array.prototype.join,';')
call()
> [['a','b'],['c','d']].map(joins)
[";", ";"]
我研究发现:
Answers explaining the thing with this and solutions equivalent to the bind shortcut I referenced
Similar explanations, the solution using thisArg again, from MDN's page about map
【问题讨论】:
-
好吧,
trim确实忽略了它传递的参数,join没有。
标签: javascript