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 comments /**/ in the function arguments ?
When a function is called with the new keyword, the function will be used as a constructor. new will do the following things: Creates a blank, plain JavaScript object. For convenience, let's call it newInstance .
What is === in JavaScript? === (Triple equals) is a strict equality comparison operator in JavaScript, which returns false for the values which are not of a similar type. This operator performs type casting for equality. If we compare 2 with “2” using ===, then it will return a false value.
To comment on a parameter, start the line with @param , followed by the parameter's name, and then a short description of what the function will do.
Function comments - Function comments are the most useful type of comments and can be automatically generated in many languages. They describe the purpose of the function, which parameters it accepts, and what output it generates.
As seen in the following Chromium issue, this is a workaround to remedy an edge case involving unbalanced block comments. 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 combined function expression
// compile. 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 comment, the
// function must be rejected. Since JavaScript does not allow nested
// comments we can include a trailing block comment to catch this.
p += '\n/' + '**/';
}
var body = (n > 0) ? ToString(arguments[n - 1]) : '';
return '(' + function_token + '(' + p + ') {\n' + body + '\n})';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With