[].reduce.call('cbaacfdeaebb',function(p,n){ return p[n] = (p[n] || 0) 1,p; },{}),
I have seen such code, but I don’t quite understand how it works. Can reduce be applied to string through call? Are there any restrictions? How does it work? Can string be changed to other types? Such as map.
JS newbie, I hope the master can explain it in detail.
仅有的幸福2017-05-19 10:42:10
JavaScript
数组方法是特意定义为通用的,因此他们不仅应用在真正的数组而且在类数组对象上也能正确工作,但是类数组对象没有继承自Array.prototype
,所以可以通过Function.call
Indirectly called.
And String
can be used as an array-like object because it has a numeric length attribute and a corresponding non-negative integer attribute.