Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 312 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Curly brackets (braces) in Node.js 'require' statement

#1
I am trying to understand the difference between the two 'require' statements below.

Specifically, what is the purpose of the `{ }`s wrapped around `ipcMain`?

const electron = require('electron')

const {ipcMain} = require('electron')

They both appear to assign the contents of the `electron` module, but they obviously function differently.

Can anyone shed some light?


Reply

#2
With `const electron = require('electron')`, the `ipcMain` module will be available as `electron.ipcMain`.

With `const {ipcMain} = require('electron')` the `ipcMain` module will be available as `ipcMain`.

This construct is called [*object destructuring*][1] and achieves the same as the Python construct

from library import ...

In its basic form it allows you to refer to the properties of an object directly:

var o = {prop1: '1', prop2: 2}
var {prop1, prop2} = o
console.log(prop1) // '1' (same as o.prop1)
console.log(prop2) // 2 (same as o.prop2)

Check:

const {ipcMain} = require('electron')
const myElectron = require('electron')
const myipcMain = myElectron.ipcMain
console.log(myipcMain===ipcMain) // true

You can use the destructuring assignment to import multiple properties of a JavaScript object, e.g.:

const { app, BrowserWindow, ipcMain } = require('electron')

If you use a property that doesn't exist, this will be set to `undefined` and you won't get an error.

const {app, BrowserWindow, ipcMain, doesntExist} = require('electron')
console.log(doesntExist) // undefined

See also: *[What does curly brackets in the `var { … } = …` statements do?][2]*

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#3
The second example uses destructuring.

This will call the specific variable (including functions) that are exported from the required module.

For example (functions.js):

module.exports = {
func1,
func2
}

is included in your file:

const { func1, func2 } = require('./functions')

Now you can call them individually,

func1()
func2()

as opposed to:

const Functions = require('./functions')

are called using dot notation:

Functions.func1()
Functions.func2()

You can read about destructuring [here][1], it is a very useful part of ES6 and can be used with arrays as well as objects.


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through