简体   繁体   中英

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.

There's an open bug report for V8.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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