这段javascript代码需要接收一个句子,提取第一个单词,并调用一个名为第一个单词的函数,但如果该单词没有函数,则不执行任何操作。但是它不是在编译吗。你知道为什么和如何修复它吗?
也就是说,我如何将参数传递给函数?
refactor = (function () {
const john = function (description) {
console.log(description);
};
const factoring = {
'john': john(description) // <--- description is not defined ---
};
return Object.freeze({
'byName': function (description) { // calls the correct private method if present
let name = description.match(/(\S+)\s/)[1];
if (name in factoring) factoring[name](description);
}
});
}());发布于 2017-04-06 10:22:40
未定义desc。可以定义desc并为其赋值(const desc = "Some random string"),也可以使用某个值('john': john("Some random string"))替换desc。
https://stackoverflow.com/questions/43244555
复制相似问题