简体   繁体   English

如何分配以函数为参数的变量

[英]How to assign a variable which function takes as a parameter

I'm trying to do something like this, but variable is not Assign ( 我正在尝试做这样的事情,但变量不是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;
}

Here's i'm trying to call this function 这是我正在尝试调用此功能

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;
}

Maybe you forgot to set a return on the final of the function: 也许您忘了在函数的最后设置返回值:

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;
}

Did you try to do this? 您尝试这样做吗?

您可以传递变量指针,或者更好地说,通过引用该函数传递str变量并对其进行更新。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将函数赋值给变量并传递参数 - Assign function to variable and pass parameter 将全局变量分配给功能参数 - Assign Global Variable to Function Parameter 将带有参数的函数作为参数传递,但是哪个参数需要调用函数的参数? - Passing a function with parameters as a parameter, but which takes the parameter of the invoking function? 将变量分配为具有相同名称的 function 参数 - Assign a variable as a function parameter with the same name 如何绑定到以功能为参数的功能 - How to bind to function that takes function as parameter 对以另一个函数作为参数的函数进行 JSDoc 注释 - Make JSDoc comment for function which takes another function as parameter 如何为调用 function 的每个单元格设置点击事件,该单元格将单元格的 id 作为 JavaScript 中的参数 - How do I set an on click event for every cell that calls a function which takes id of cell as a parameter in JavaScript 如何定义一个以数字为参数并以角度返回它的正方形的函数? - How to define a function which takes a number as parameter and returns it's square, in angular? 如何将我通过函数传递的变量绑定到特定参数 - How to bound variable which I pass through function to a specific parameter 编写一个函数来打印接受这个 wa 中的参数的总和: sum(2)(3) 并输出 5 - Write a function to print the sum which takes the parameter in this wa: sum(2)(3) and outputs 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM