admin管理员组文章数量:1355559
in chrome 47 and nodejs v0.12
new Function('myArg','return "my function body";')
gives the following results :
function anonymous(myArg /**/) {
return "my function body"
}
why is there ments /**/ in the function arguments ?
in chrome 47 and nodejs v0.12
new Function('myArg','return "my function body";')
gives the following results :
function anonymous(myArg /**/) {
return "my function body"
}
why is there ments /**/ in the function arguments ?
Share Improve this question asked Oct 20, 2015 at 15:07 fadomirefadomire 1,9751 gold badge15 silver badges23 bronze badges 13- 5 They needed it to fix the arguments parser for ES6. Wait, I'll dig up the bug discussion – Bergi Commented Oct 20, 2015 at 15:11
- 3 Here's the short explanation (as ment) in the source of the V8 engine: "If the formal parameters include an unbalanced block ment, the function must be rejected. Since JavaScript does not allow nested ments we can include a trailing block ment to catch this." – Andreas Commented Oct 20, 2015 at 15:13
-
1
So, it's just a way of making
'myArg /*'
not throw an error? or what. – Kevin B Commented Oct 20, 2015 at 15:17 - 1 @KevinB yes it seems to be the reason. i'm wondering why it don't throw an error in case you put an unblanced ment instead of allowing it and fixing it with this workaround – fadomire Commented Oct 20, 2015 at 15:25
- 2 Ah, I was actually refering to bugzilla.mozilla/show_bug.cgi?id=755821 - seems Firefox is going to do the same – Bergi Commented Oct 20, 2015 at 16:24
2 Answers
Reset to default 8As seen in the following Chromium issue, this is a workaround to remedy an edge case involving unbalanced block ments. As described in the V8 source code:
function NewFunctionString(arguments, function_token) {
var n = arguments.length;
var p = '';
if (n > 1) {
p = ToString(arguments[0]);
for (var i = 1; i < n - 1; i++) {
p += ',' + ToString(arguments[i]);
}
// If the formal parameters string include ) - an illegal
// character - it may make the bined function expression
// pile. We avoid this problem by checking for this early on.
if (%_CallFunction(p, ')', StringIndexOfJS) != -1) {
throw MakeSyntaxError('paren_in_arg_string', []);
}
// If the formal parameters include an unbalanced block ment, the
// function must be rejected. Since JavaScript does not allow nested
// ments we can include a trailing block ment to catch this.
p += '\n/' + '**/';
}
var body = (n > 0) ? ToString(arguments[n - 1]) : '';
return '(' + function_token + '(' + p + ') {\n' + body + '\n})';
}
This was originally added to catch cases like the following and throw an error:
Function("/*", "*/){alert('bad');")
This should result in a syntax error, but before they added the additional /**/
, it would be translated to:
function anonymous(/*) {
*/){alert('bad');
}
Which is equivalent to
function anonymous(/*...*/) {
alert('bad');
}
And hence no syntax error. After the change, with the extra ment this now bees:
function anonymous(/*/**/) {
*/){alert('bad');
}
which correctly gives a syntax error:
Uncaught SyntaxError: Unexpected token *(…)
Source revision and original bug report.
本文标签: javascriptwhy new Function() return comments ** in argumentsStack Overflow
版权声明:本文标题:javascript - why new Function() return comments ** in arguments? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744003597a2574301.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论