A默苍离 2020-08-01 08:57 采纳率: 0%
浏览 103
已采纳

关于node.js export 的问题

main.js

var fooExports = require('./foo')
console.log(fooExports.foo)
console.log(fooExports)

foo.js

var foo = 'bar'

function add(x, y) {
  return x + y
}

module.exports.foo = '挂载:hello'

module.exports = 'hello' 


module.exports = function (x, y) {   
  return x + y
}

module.exports = {  
    return x + y
  },
  str: 'hello'
}

//module.exports.foo = '挂载:hello'

执行结果:

undefined
{ add: [Function: add], str: 'hello' }

main.js

var fooExports = require('./foo')
console.log(fooExports.foo)
console.log(fooExports)

foo.js

var foo = 'bar'

function add(x, y) {
  return x + y
}

module.exports.foo = '挂载:hello'

module.exports = 'hello' 


module.exports = function (x, y) {   
  return x + y
}

module.exports = {  
    return x + y
  },
  str: 'hello'
}

module.exports.foo = '挂载:hello'

执行结果:

挂载:hello
{ add: [Function: add], str: 'hello', foo: '挂载:hello' }

为什么第一次执行结果会出现 undefined?

  • 写回答

1条回答 默认 最新

  • jingluan666 2020-08-01 09:27
    关注

    上面的module.exports被定义多次(多次赋值),那么就以最后的一次为准

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 questasim仿真报错
  • ¥15 寻找电脑攻防的导师,有问题请教一下。
  • ¥20 微信同是win11,我的电脑安装不了pageoffice,一直无法打开
  • ¥15 这个界面我通过postman请求不到,但是通过浏览器可以正常访问
  • ¥15 动态规划算法实现背包问题
  • ¥15 wpf程序使用过程中异常奔溃
  • ¥15 多目标优化算法在与其他算法数据对比结果判断
  • ¥15 CPTN和EAST,主干网络是VGG16,请问在ICDAR2015数据集上训练之后,CPTN和EAST模型的大小为多少
  • ¥15 按颜色进行点云分割-python
  • ¥15 Matlab如何实现汽车变道切入场景的批量仿真