function normalize() { console.log(this.coords.map(function(x){ return x/this.length; })); } normalize.call({coords: [0, 2, 3], length: 5});
Ожидаемый результат: [0,0.4,0.6]
Выход: [NaN, Infinity, Infinity]
Может кто-нибудь объяснить ошибку?
Вам необходимо взять this вместе с функцией отображения с Array#map.Без thisArg обратный вызов не имеет доступа к this.
this
Array#map
thisArg
function normalize() { return this.coords.map(function (x) { return x/this.length; }, this); } console.log(normalize.call({ coords: [0, 2, 3], length: 5 }));