-1
function sample(banana)
       {return new Function('return ' + banana)() }

为什么函数后面有两个括号?

第一个括号是新创建函数的参数。?第二个括号有什么作用?

4

2 回答 2

0

分步考虑可能会有所帮助:

function sample(banana) {

  // Create a new function that can be invoked later
  var myNewFunc = new Function('return ' + banana)

  // Invoke the function
  var result = myNewFunc()

  return result

}

现在开始删除额外的步骤。

首先返回调用函数的结果而不存储在单独的变量中:

function sample(banana) {

  // Create a new function that can be invoked later
  var myNewFunc = new Function('return ' + banana)

  // Invoke the function and return the result
  return myNewFunc()

}

现在您可以删除存储函数的变量:

function sample(banana) {

  // Create a new function and invoke it immediately
  return new Function('return ' + banana)()

}
于 2019-10-17T07:02:34.670 回答
0

您可以调用带括号的函数()

x = new Function('return 0');
console.log(x());


//...is same as

console.log(new Function('return 0')())

于 2019-10-17T07:01:39.413 回答