An error was reported today while executing the following section

xxx

[1.2.3].forEach(function(val) {
  console.log(val)
})
Copy the code

TypeError: Cannot read property ‘forEach’ of undefined

[] is recognized as an attribute reader

For a second look at Javascript’s semicolon insertion rule, Hacking Semicolons and An Open Letter to Javascript Leaders Regarding Semicolons

Write down your own understanding below

The general rule of no semicolons

In general, carriage return (\n) is the end of a statement, unless:

  1. Statements have unclosed parentheses, array literals, object literals, or a non-valid closing character (e.g.and.)
  2. The contents of this trip are++or--(Will increase/decrease a token(word?) )
  3. for(),while(),do,if()orelseNo back{
  4. The next line[,(,+,-,*,/,.Or some other unary operator that can be used between two tokens

Article 2:

i
++
j
Copy the code

Resolves to I; ++j The statement structure of the third clause ends when it encounters a statement or block of code

The fourth problem I’m having, if I don’t want to be associated with the previous statement, add one;

foo();
[1.2.3].forEach(bar)
Copy the code

or

foo() ; [1.2.3].forEach(bar)
Copy the code

Restricted Productions

Yield for return, throw, break, continue, ES6, and statements suffixed with ++, –, followed by carriage return. The statement ends with a semicolon

return
{
  a: 1
}
Copy the code

Is equivalent to

return;
{
  a: 1
}
Copy the code
(function () {
  let a = 6
  a++ // No; It's also unambiguous
  [1.2.3].forEach(v= > console.log(v))
})()
Copy the code

The original text is in English, understanding may have some deviation, welcome correction, thank you