gpt4 book ai didi

javascript - 得到意想不到的输出

转载 作者:行者123 更新时间:2023-11-30 14:34:39 25 4
gpt4 key购买 nike

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

function normalize() {
return this.coords.map(function (x) {
return x/this.length;
}, this);
}

console.log(normalize.call({ coords: [0, 2, 3], length: 5 }));

关于javascript - 得到意想不到的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50599064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com