javascript 模块化(导入和导出文件)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 模块化(导入和导出文件)相关的知识,希望对你有一定的参考价值。

// Using the require keyword lets us access all of the exports
// in our ess.js file
var stuffINeed = require("./ess.js");

// This will print everything in exports.
console.log("--------------------------");
console.log("ALL THE STUFF I NEED");
console.log(stuffINeed);
console.log("--------------------------");

// These will print correctly because we imported them
console.log("Essentials");
console.log(stuffINeed.essentials);
console.log("--------------------------");
console.log("Nice to Haves");
console.log(stuffINeed.niceToHaves);

// This won't print anything because it wasn't exported in ess.js
console.log("--------------------------");
console.log("Nonessentials");
console.log(stuffINeed.nonessentials);
var essentials = {
  drink: "water",
  eat: "snickers",
  fun: "phone",
  friend: "facebook"
};

var niceToHaves = {
  tools: ["can opener", "flashlight", "matches"],
  safety: ["first aid kit", "gloves"]
};

var nonessentials = {
  cookware: "waffleMaker"
};

// module.exports is essentially an object that we can add data or variables to
// We can access them from other files using the 'require' keyword.

module.exports = {
  essentials: essentials,
  niceToHaves: niceToHaves
};

以上是关于javascript 模块化(导入和导出文件)的主要内容,如果未能解决你的问题,请参考以下文章

javascript模块导入导出

通过导入和导出制作最少的工作 JavaScript 代码

javascript 导出和导入模块

javascript 模块导入导出

Es6中的模块化Module,导入(import)导出(export)

LayaBox---TypeScript---模块