0

In javascript,I import a named object like this:

import {my_object} from './my_js.js'

Is some sort of destructuring happening in this above mentioned import statement or using curly braces is just a requirement for importing named object?

thanks in advance

KawaiKx
  • 9,558
  • 19
  • 72
  • 111
  • 3
    Imports don't destructure. The syntax may look similar, but it's very much not the same thing. Imports _only_ import. If you use `{...}` you're importing _named_ exports from whatever library you're importing from. If `my_js` has an export for something that is named `my_object` (a `const`, or a `function`, or etc.) then that's what gets imported. – Mike 'Pomax' Kamermans Jul 12 '22 at 04:44

1 Answers1

2

Although the syntax looks very similar to destructuring, and the operation being performed is somewhat similar to destructuring (in that it takes the my_object binding from the namespace and puts it into an identifier), it's something quite distinct. For a couple of examples...

With destructuring, you can destructure nested properties of an object. This is impossible to do (in one statement) with imports.

With destructuring, you can put the property into a different variable with a colon. With imports, as must be used instead.

const { prop: propIdentifier } = obj;
import { theName as theNameIdentifier } from './somefile';

using curly braces is just a requirement for importing named object?

Exactly.

There are things you can do with destructuring that you can't do when importing, and there are things you can do when importing that you can't do with destructuring.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320