Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
24
rated 0 times [  30] [ 6]  / answers: 1 / hits: 26156  / 8 Years ago, fri, july 29, 2016, 12:00:00

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?


More From » node.js

 Answers
112

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, it is a very useful part of ES6 and can be used with arrays as well as objects.


[#61207] Wednesday, July 27, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
iliana

Total Points: 246
Total Questions: 109
Total Answers: 82

Location: Palestine
Member since Tue, Jul 20, 2021
3 Years ago
;