How to understand this code about js permutation and combination?

Code:

/**
  * 
  **/
bonusCombination (arr, num, fun) {
  if (arr.length < num || num > 10) {
    return []
  }
  let variable = ["a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u"]
  let replaceStr = "-sharp$-sharp"
  let str = "var arr=arguments[0]; var fun=arguments[1];  var ret=[]; for (var a = 0; a < arr.length; aPP) { " + replaceStr + " } return ret;"
  for (let i = 1; i < num; iPP) {
    str = str.replace(replaceStr, " for (var " + variable[i] + " =" + variable[i - 1] + "+ 1; " + variable[i] + " < arr.length; " + variable[i] + "PP) { " + replaceStr + "  }")
  }
  let temp = "var temp= [];"
  for (let i = 0; i < num; iPP) {
    temp += "temp.push(arr[" + variable[i] + "]);"
  }
  if (fun) {
    temp += "ret.push(fun(temp)); "
  } else {
    temp += "ret.push(temp);"
  }
  str = str.replace(replaceStr, temp)
  return (new Function(str)).apply(null, [arr, fun])
}

figure:

  1. doesn"t understand why strings are used as variables. Is there any other function of this method?
  2. Why did you define a variable; in the first place
  3. Please explain step by step.

explain the source code one by one, which is probably not easy to understand; let's look at this from another point of view.

< H2 > returns a dynamic function < / H2 >

first look at the return statement of the bonusCombination function: return (new Function (str)). Apply (null, [arr, fun]) . The decomposition of this statement is as follows::

  • new Function (str) : it means to dynamically construct a function using the str string. Let's call the constructed function strFn (that is, var strFn = new Function (str) )
  • .
  • so there is strFn.apply (null, [arr, fun]) , which is transformed into a form that we can usually understand as strFn (arr, fun) .

from this we can see that the purpose of this bonusCombination is to dynamically build a function strFn , which accepts two input parameters arr and fun -- which are the input parameters we passed to bonusCombination ;

corresponding to the example you gave, the arr here is . , while fun is null

. < H2 > input the function of num and dynamically create the function body < / H2 >

We know that bonusCombination has another input parameter, which is num . Its function is to forge the specific content of the above dynamic function strFn , and use this num input parameter to influence that large segment of character operation, thus affecting the specific content of the dynamic function:

.

for reading such a function, the best way to understand what it means is to enumerate.

for example, if we let num be 2 : then the dynamically built function strFn has the following structure:

  

is it so slow to run?

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bdbc49-3129d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bdbc49-3129d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?