I know we can't use Array.prototype.map on strings like
const str = 'MY NAME IS USER';
const result = str.map(c => c); // "Uncaught TypeError: str.map is not a function"
console.log(result);
This will result in error states:
"Uncaught TypeError: str.map is not a function"
But today I came across a code snipet where I can use call Array.prototype on strings
const str = 'MY NAME IS USER';
const x = Array.prototype.map
.call(str, (c, i) => {
if (str.indexOf(c, i + 1) == -1 && str.lastIndexOf(c, i - 1) == -1)
return c;
})
.join('');
console.log(x);
In this snippet I couldn't get how Array.prototype.map called on string str.