//example.js var n = 1; function sayHello( name ){ var name = name || "Tom"; return "Hello~"+name } function addFn(val){ var val = val.x+val.y; return val } module.exports ={ n:n, sayHello:sayHello, addFn:addFn }
使用requier()引入使用
1 2 3 4 5 6 7 8 9 10
//main.js var example = require('./example.js'); var addNum = { "x":10, "y":5 } console.log( example )//查看example输出的对外模块接口; console.log( example.n )//1; console.log( example.sayHello("Jack") )// "Hello~ Jack"; console.log( example.addFn(addNum) ) //15;