我正在尝试做这样的事情,但是变量不是 Assign (
const insert = (str, index, pasteString) => {
let res;
if (index > 0) {
res = str.substring(0, index) + pasteString + str.substring(index, str.length);
} else {
res = pasteString + str;
}
str = res;
}
我正在尝试调用这个函数
const filterDescription = (obj) => {
const str = obj.description;
const strLen = str.length;
const fifty = Math.ceil(regexIndexOf(/[\/.!;?]/, str, strLen / 2));
const oneOfFour = Math.ceil(regexIndexOf(/[\/.!;?]/, str, strLen / 4));
const oneOfThree = Math.ceil(regexIndexOf(/[\/.!;?]/, str, strLen / 3));
console.log([fifty, oneOfFour, oneOfThree]);
insert(str, fifty, `string`);
insert(str, oneOfFour, `string`);
insert(str, oneOfThree, `string`);
insert(str, 1, `string`);
insert(str, strLen - 1, `string`);
return str;
}
最佳答案
也许你忘了在函数的最后设置一个返回:
const insert = (str, index, pasteString) => {
let res;
if (index > 0) {
res = str.substring(0, index) + pasteString + str.substring(index, str.length);
} else {
res = pasteString + str;
}
return res;
}
你试过这样做吗?
关于javascript - 如何分配函数作为参数的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53715535/