Call methods on native Javascript types without wrapping with ()

前端 未结 5 1677
暗喜
暗喜 2021-01-14 08:22

In Javascript, we can call methods on string literals directly without enclosing it within round brackets. But not for other types such as numbers, or functions. It is a syn

相关标签:
5条回答
  • 2021-01-14 08:41

    I prefer to use parenthesis to disambiguate:

    (7).alert();
    
    0 讨论(0)
  • 2021-01-14 08:44

    The lexer is expecting a decimal value when it sees a number immediately proceeded by a period. Though you could get "creative" and put a space between the number, as in 7 .alert() but people would probably hunt you down for it when they get stuck maintaining your code!

    0 讨论(0)
  • 2021-01-14 08:44

    Use the decimal point where the number expects it, and the dot operator will work as the object expects it.

    7.0.toFixed(0)

    0 讨论(0)
  • 2021-01-14 08:47

    For the Number literal all other answers have pointed you in the right direction, 7. is a valid DecimalLiteral.

    The grammar tells you that the decimal digits after the first dot are optional:

    DecimalLiteral ::
    DecimalIntegerLiteral . DecimalDigitsopt ExponentPartopt 
    

    * Note the opt suffix

    Now, for the function, the problem is that it is being evaluated in statement context.

    There are two valid grammatical ways to create function objects (the third way to create functions is using the Function constructor, but for now it's out of the topic).

    FunctionDeclaration:

    function name(/*[param] [, param] [..., param]*/) {
       // statements
    }
    

    Function Expression:

    var foo = function /*nameopt*/(/*[param] [, param] [..., param]*/) {
      // statements
    };
    

    The grammar is almost identical, the difference is where the function keyword appears.

    A function declaration occurs when the function keyword is found directly on global code or in the FunctionBody of a function.

    A function expression occurs then the function keyword is found on a expression context, like in the above example, the second function is part of an AssignmentExpression.

    But you have actually two problems, first, the name of a FunctionDeclaration is mandatory.

    Second, when the FunctionDeclaration statement is evaluated the dot will simply cause a SyntaxError because the dot isn't expected.

    Wrapping the function between parentheses (formally called The Grouping Operator) makes the function to be evaluated in expression context, a function expression.

    For example, the following is valid:

    0,function () {}.alert();
    

    The above works because the comma operator evaluates expressions.

    You should know also that the FunctionDeclarations are evaluated at "parse time" (more precisely, when the control enters into an Execution Context, by the Variable Instantiation process), and the identifiers (function names) are made available to the entire current scope, for example:

    foo(); // alerts "foo", function made available at 'parse time'
    foo = function () { alert('bar') }; // override with a FunctionExpression
    function foo () { alert('foo'); } // FunctionDeclaration
    foo(); // alerts "bar", the overriden function
    

    Recommended article:

    • Named function expressions demystified
    0 讨论(0)
  • 2021-01-14 08:51

    Hint: 7. is a valid numerical constant.

    0 讨论(0)
提交回复
热议问题