Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Array.prototype.map(回调,thisArg),忽略第二个参数_Javascript_Node.js - Fatal编程技术网

Javascript Array.prototype.map(回调,thisArg),忽略第二个参数

Javascript Array.prototype.map(回调,thisArg),忽略第二个参数,javascript,node.js,Javascript,Node.js,我正在使用Node.js编写一个小游戏,我希望它能以两种不同的语言提供 为了显示不同游戏模式的翻译列表及其各自的描述,我使用了Array.prototype.map(callback,thisArg),但Node似乎忽略了thisArg参数: sendMessage(translated[channel.id].modeList + modes.filter(a => { if (typeof a === "string") return true; ret

我正在使用Node.js编写一个小游戏,我希望它能以两种不同的语言提供

为了显示不同游戏模式的翻译列表及其各自的描述,我使用了
Array.prototype.map(callback,thisArg)
,但Node似乎忽略了
thisArg
参数:

sendMessage(translated[channel.id].modeList + modes.filter(a => {
    if (typeof a === "string")
        return true;
    return false;
}).map(translated.modeDesc, translated[channel.id]).join("\n"));
翻译后的:

const translated = {
    "chooseLang" : "Choose a language",
    "invalidOption" : "Invalid option",
    "modeDesc" : mode => {return mode + " : " + "<" + modes[0][mode].join("|") + ">\n = " + this.modeDescs[mode];},
    "(channel id)" : global.en
};
节点似乎正在尝试使用不存在的
translated.modeDescs
,而不是
translated[channel.id].modeDescs
global.en.modeDescs
):

那么,Node真的忽略了thisArg吗?还是我的方式不对?我该怎么做才能有我想要的行为

提前感谢。

使用时,保留词法范围,因此
指的是
已翻译的
对象在中定义的上下文,而不是包含函数引用的实际对象

尝试使用常规函数:

"modeDesc" : function(mode) {return mode + " : " + "<" + modes[0][mode].join("|") + ">\n = " + this.modeDescs[mode];}

嗯,这很有效。我不知道箭头函数的特殊行为,谢谢。
TypeError: Cannot read property 'mode' of undefined
    at Object.ruleDesc (/home/algorythmis/Documents/Game/main.js:89:111)
    at Array.map (native)
    ...
"modeDesc" : function(mode) {return mode + " : " + "<" + modes[0][mode].join("|") + ">\n = " + this.modeDescs[mode];}
var data = translated[channel.id].modeList + modes.filter(a => { ... });
data = Array.prototype.map.call(translated, translated.modeDesc);

sendMessage(data);