Since there's no semicolon after the function, the snippet gets interpreted as the following:
const print2 = function(x, y) {
console.log(x*y)
}[1,2,3,4].forEach( x => print2(x, 20) )
Which means that it's trying to index into the function. Add a semicolon either after the function, or before the array literal:
const print2 = function(x, y) {
console.log(x*y)
};
[1,2,3,4].forEach( x => print2(x, 20) )
or
const print2 = function(x, y) {
console.log(x*y)
}
;[1,2,3,4].forEach( x => print2(x, 20) )
More about Javascript's automatic semicolon insertion here: What are the rules for JavaScript's automatic semicolon insertion (ASI)?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…