解构还是不同的东西?
Destructuring or Something Different?
这看起来像解构:
const {getElementById, seedElements} = require('./utils')
但我对此感到困惑。我习惯于看到类似的东西:
let {first, last} = name
它们只是在不同的文件中做同样的事情吗?
你可以想
const {getElementById, seedElements} = require('./utils')
作为解构,因为当你导出时,你会像这样写你的导出
module.exports = { getElementById, seedElements };
或
export { getElementById, seedElements };
并且在使用 require 导入时,您基本上会导入整个模块,并且您可以从中解构各个模块。
const {getElementById, seedElements} = require('./utils')
类似于
const Utils = require('./utils');
const { getElementById, seedElements } = Utils;
使用导入语法,您将导入命名导出,例如
import { getElementById, seedElements } from './utils';
对,就是对象解构。
require()
function in Node.js can be used to import modules, JSON, and local files. For instance (from the docs):
// Importing a local module:
const myLocalModule = require('./path/myLocalModule');
调用 require(moduleId)
returns moduleId
的对象 module.exports
(module.exports
恰好包含模块可用的所有属性)。
这看起来像解构:
const {getElementById, seedElements} = require('./utils')
但我对此感到困惑。我习惯于看到类似的东西:
let {first, last} = name
它们只是在不同的文件中做同样的事情吗?
你可以想
const {getElementById, seedElements} = require('./utils')
作为解构,因为当你导出时,你会像这样写你的导出
module.exports = { getElementById, seedElements };
或
export { getElementById, seedElements };
并且在使用 require 导入时,您基本上会导入整个模块,并且您可以从中解构各个模块。
const {getElementById, seedElements} = require('./utils')
类似于
const Utils = require('./utils');
const { getElementById, seedElements } = Utils;
使用导入语法,您将导入命名导出,例如
import { getElementById, seedElements } from './utils';
对,就是对象解构。
require()
function in Node.js can be used to import modules, JSON, and local files. For instance (from the docs):
// Importing a local module:
const myLocalModule = require('./path/myLocalModule');
调用 require(moduleId)
returns moduleId
的对象 module.exports
(module.exports
恰好包含模块可用的所有属性)。