javascript - Array.from doesn't work as a direct callback function in Array#map -
i've discovered odd behavior array.from. appears not work callback function directly when mapped on array of array-like objects. i've tested in chrome.
here's test code (es6):
const fails = () => { const x = { 0: 'help', length: 1 }; const y = [x].map(array.from); // throw error return y; }; const works = () => { const x = { 0: 'help', length: 1 }; const y = [x].map(item => array.from(item)); // work return y; }; console.log(works()); console.log(fails()); https://jsfiddle.net/dox6wnya/
this peculiar behavior. i'm wondering why happens.
.map passes three arguments callback (currentvalue, index, array), , .from accepts 3 arguments (arraylike, mapfn, thisarg). types of arguments don't match and/or produce unexpected results; in particular "0 not function", 0 index argument passed mapfn. real compatible argument first one, why it's 1 should pass.
Comments
Post a Comment