为什么此代码记录 'undefined'?
Why does this code log 'undefined'?
我的包 json 配置为使用 ES6 模块使用 babel:
"scripts": {
"start": "nodemon src/index.js --exec babel-node --presets es2015,stage-2",
"build": "babel src -d dist",
"serve": "node dist/index.js"
},
这是我的索引文件:
import test from './test'
console.log(test)
console.log(test())
测试文件:
let test = () => console.log('test');
export default test;
当我运行代码时,我得到:
[Function: test]
test
undefined
undefined从哪里来?
您正在记录函数的 return 值:
console.log(test())
您首先调用 test
,它会记录 'test'
。接下来,由于 console.log
没有 return 任何东西,test
没有 return 任何东西所以 return 值是 undefined
这就是已记录。
因为 test () 没有返回您尝试登录控制台的任何类型。即为什么它在控制台
中显示未定义
我的包 json 配置为使用 ES6 模块使用 babel:
"scripts": {
"start": "nodemon src/index.js --exec babel-node --presets es2015,stage-2",
"build": "babel src -d dist",
"serve": "node dist/index.js"
},
这是我的索引文件:
import test from './test'
console.log(test)
console.log(test())
测试文件:
let test = () => console.log('test');
export default test;
当我运行代码时,我得到:
[Function: test]
test
undefined
undefined从哪里来?
您正在记录函数的 return 值:
console.log(test())
您首先调用 test
,它会记录 'test'
。接下来,由于 console.log
没有 return 任何东西,test
没有 return 任何东西所以 return 值是 undefined
这就是已记录。
因为 test () 没有返回您尝试登录控制台的任何类型。即为什么它在控制台
中显示未定义