Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why new Function() return comments /**/ in 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 comments /**/ in the function arguments ?

like image 882
fadomire Avatar asked Oct 20 '15 15:10

fadomire


People also ask

What is the function of new?

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 does === return in JavaScript?

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.

How do you comment a function parameter?

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.

What is a function comment?

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.


1 Answers

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})';
}
like image 122
Griffith Avatar answered Oct 02 '22 13:10

Griffith