简体   繁体   English

为什么“函数”的第一个参数拒绝括号?

[英]Why does first argument of `Function` deny parentheses?

To create this function dynamically, 要动态创建此功能,

var foo = function(argument = myFunc()){
    return argument
}

I wrote this code. 我写了这段代码。

var funcArgument = "argument = myFunc()"
var funcBody = "return argument"
var foo = Function(funcArgument, funcBody)

However, I got a following error. 但是,出现以下错误。

Function arg string contains parenthesis

Why does this limitation exist? 为什么存在此限制?

In order to avoid this limitation, it can be written as follows, but it is too tricky and is not preferable. 为了避免这种限制,可以将其编写如下,但是它太棘手了,不是可取的。

var funcArgument = "argument = Function`return myFunc\\u0028\\u0029```"
var funcBody = "return argument"
var foo = Function(funcArgument, funcBody)

Why does this limitation exist? 为什么存在此限制?

There's no good reason, this should work according to the spec. 没有充分的理由,这应该符合规范。 In fact it does work in Firefox. 实际上,它确实可以在Firefox中运行。

There's an open bug report for V8. V8有一个开放的错误报告

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM