// main.js
const otherModule = require('./other.js');
// 调用 other.js 中的函数
otherModule.sayHello();
// other.js
module.exports = {
sayHello: function() {
console.log('Hello from other.js');
}
};
main.js:
require 方法引用同一目录下的 other.js 文件。other.js 中导出的 sayHello 函数。other.js:
module.exports 导出一个对象,该对象包含一个名为 sayHello 的函数。main.js 调用 otherModule.sayHello() 时,会执行 other.js 中定义的 sayHello 函数,并在控制台输出 Hello from other.js。上一篇:js 模块化
下一篇:js 写文件
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站