[英]How do I import many variables at once in javascript in NodeJS?
假設我有一個javascript模塊first_file.js
:
var first = "first",
second = "second",
third = "third";
module.exports = {first, second, third};
如何將這些文件導入一行中的另一個文件? 以下僅導入third
:
var first, second, third = require('./path/to/first_file.js');
您正在導出具有這些屬性的對象。 您可以直接使用該對象獲取它們:
var obj = require('./path/to/first_file.js');
obj.first;
obj.second;
obj.third;
或者使用解構:
var { first, second, third } = require('./path/to/first_file.js');
從版本4.1.1開始,Node.js尚不支持開箱即用的解構。
在ES6(ECMAScript 2015)中,您可以使用對象解構:
const { first, second, third } = require('./path/to/first_file.js');
您可以將它們存儲在一個數組中:
module.exports = [first, second, third];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.