看雪峰老师的ECMAScript6入门,8-函数扩展中提到:
// 箭头函数写法
[1,2,3].map(x => x * x);
编译时通过,但运行时出现错误,但:
let list = [1,2,3]
list.map( x => x * x );
// OK
let list2 = [1,2,3].map( (x) => x*x )
console.log( list2 )
就正确了,请问为什么??
[1,2,3].forEach( (x) => console.log( x*x ) )
也是错误
// 错误
[Error] TypeError: 'GAME INIT'[(1, 2, 3)].map is not a function. (In ''GAME INIT'[(1, 2, 3)].map(function (x) {
return x * x;
})', ''GAME INIT'[(1, 2, 3)].map' is undefined)
// 我单独在JSbin上测试了出现错误 error
// package.json (由vue-cli建立的)
"babel-core": "^6.22.1",
"babel-eslint": "^7.1.1",
"babel-loader": "^6.2.10",
"babel-plugin-istanbul": "^3.1.2",
"babel-plugin-transform-runtime": "^6.22.0",
"babel-polyfill": "^6.23.0",
"babel-preset-latest": "^6.22.0",
"babel-preset-stage-2": "^6.22.0",
"babel-register": "^6.22.0",
你将计算的结果赋值给一个变量就不会出错了