Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

nodejs 引用其他js

作者:淡情   发布日期:2025-12-13   浏览:63

// main.js
const otherModule = require('./other.js');

// 调用 other.js 中的函数
otherModule.sayHello();

// other.js
module.exports = {
  sayHello: function() {
    console.log('Hello from other.js');
  }
};

解释说明:

  1. main.js:

    • 使用 require 方法引用同一目录下的 other.js 文件。
    • 调用 other.js 中导出的 sayHello 函数。
  2. other.js:

    • 使用 module.exports 导出一个对象,该对象包含一个名为 sayHello 的函数。
    • main.js 调用 otherModule.sayHello() 时,会执行 other.js 中定义的 sayHello 函数,并在控制台输出 Hello from other.js

上一篇:js 模块化

下一篇:js 写文件

大家都在看

js 数组对象排序

js 数组删掉第一个值

js fill

js 数组连接

js json数组

js 数组复制

js 复制数组

js 数组拷贝

js 对象数组合并

js 对象转数组

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站