[英]module.exports a function in nodejs
I want to export function and variable from one file ( module ) to another . 我想将功能和变量从一个文件(模块)导出到另一个文件(模块)。 This is how it is 就是这样
// animals.js
function weight () {
return "90kgs";
}
module.exports = weight();
// tiger.js
var animal = require('./animals.js');
module.exports = {
'animalWeight' : function animal.weight(),
'stripes' : true
}
// zoo.js
var tiger = require('./tiger.js');
tiger.animalWeight(); // should return 90kgs
tiger.stripes ; // should return true
How to achieve above. 如何实现以上。 I get following error 我收到以下错误
'animalWeight' : function animal.weight(),
^
SyntaxError: Unexpected token .
When you export a function, you'd reference it 导出函数时,需要引用它
function weight () {
return "90kgs";
}
module.exports = weight;
now when you import it, you get that function, and can reference it again 现在,当您导入它时,您将获得该函数,并可以再次引用它
var animal = require('./animals.js');
module.exports = {
'animalWeight' : animal,
'stripes' : true
}
and when you import that again, you can call that function 当您再次导入时,可以调用该函数
var tiger = require('./tiger.js');
tiger.animalWeight(); // "90kgs"
tiger.stripes ; // true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.