0

This looks like destructuring:

const {getElementById, seedElements} = require('./utils')

but I'm confused about it. I'm used to seeing something like:

let {first, last} = name

Are these doing the same things just in different files?

  • https://stackoverflow.com/questions/38660022/curly-brackets-braces-in-node-require-statement it's the same when used for require / import objects. – zebnat Sep 25 '18 at 16:19
  • Possible duplicate of [What does curly brackets in the \`var { ... } = ...\` statements do?](https://stackoverflow.com/questions/15290981/what-does-curly-brackets-in-the-var-statements-do) – user2314737 Jan 01 '19 at 18:59

2 Answers2

1

You can think

const {getElementById, seedElements} = require('./utils')

as destructuring since when you export, you would write your export like

module.exports = { getElementById, seedElements };

or

export { getElementById, seedElements };

and while importing using require you would basically be importing the entire module and you can destructure the individual modules from it.

const {getElementById, seedElements} = require('./utils')

would be similar to

const Utils = require('./utils');
const { getElementById, seedElements } = Utils;

with the import syntax, you would however import the named exports like

import { getElementById, seedElements } from './utils';
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
0

Yes, that is object destructuring.

The 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');

Calling require(moduleId) returns the object module.exports of moduleId ( module.exports contains precisely all properties that are made available by the module).

user2314737
  • 27,088
  • 20
  • 102
  • 114