10

What is the difference? I looked at the ECMAScript specification, but did not understand. The real code examples that would help much.

If you can explain every line here it would be nice

MemberExpression : 
 PrimaryExpression 
 FunctionExpression
 MemberExpression [ Expression ] 
 MemberExpression . IdentifierName 

CallExpression : 
 MemberExpression Arguments
 CallExpression Arguments 
 CallExpression [ Expression ] 
 CallExpression . IdentifierName

For example

console.log - MemberExpression: MemberExpression . IdentifierName
console.log("hi") - CallExpression : MemberExpression Arguments

What is will be equal

CallExpression : CallExpression . IdentifierName
CallExpression [ Expression ]
CallExpression : CallExpression Arguments 

Link for ES http://www.ecma-international.org/ecma-262/5.1/#sec-11.2

MaximPro
  • 563
  • 8
  • 21

2 Answers2

17
  • func() is a CallExpression
  • thing.func is a MemberExpression
    • thing is the object of the MemberExpression
    • func is the property of the MemberExpression
  • thing.func() is a MemberExpression within a CallExpression
    • thing.func is the callee of the CallExpression

Source: astexplorer.net.

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
  • Can you give me explanations with this http://www.ecma-international.org/ecma-262/5.1/#sec-11.2 (about every line MemberExpression and CallExpression) Especially **MemberExpression [ Expression ]** and **CallExpression [ Expression ]** or **MemberExpression . Expression** and **CallExpression . Expression** – MaximPro Dec 26 '17 at 00:46
  • They're defining the grammar of various `Expressions` by enumerating what other expressions they can encompass. If you want to learn what they _all_ mean, refer to the AST specification of a JavaScript parser such as [babylon](https://github.com/babel/babylon/blob/master/ast/spec.md). – Patrick Roberts Dec 26 '17 at 00:52
4

The relevant parts here are

NewExpression:
    MemberExpression
    new NewExpression
LeftHandSideExpression:
    NewExpression
    CallExpression

which distinguishes the three major left hand side expressions:

  • constructor calls
  • function/method calls
  • primary expressions

And all of them with member accesses in the right places. As such, the difference between the productions you listed is just that a CallExpression always contains a call - and may therefore not be part of the expression after a newoperator.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    Explain to me the difference between `MemberExpression . IdentifierName` and `CallExpression . IdentifierName` – MaximPro Apr 01 '18 at 05:55
  • I readed it! But can you give me example for `CallExpression . IdentifierName` or `CallExpression Arguments` I don't imagine this, how it will be (`MemberExpression . IdentifierName` or `MemberExpression Arguments` I do imagine it). – MaximPro Apr 01 '18 at 07:38
  • @MaximPro `member().callProperty` and `member()()` (vs `member.memberProperty` and `member()`). – Bergi Apr 01 '18 at 10:39
  • Interesting. `MemberExpression Arguments` - Its `CallExpression` Right? – MaximPro Apr 01 '18 at 11:01
  • Yes, that's what your question says: `CallExpression : MemberExpression Arguments` – Bergi Apr 01 '18 at 17:47