Node.js'require'语句中的花括号(大括号) [英] Curly brackets (braces) in Node.js 'require' statement
问题描述
我试图理解下面两个'require'语句之间的区别。
I am trying to understand the difference between the two 'require' statements below.
具体来说, {} $ c的目的是什么$ c>包裹在
ipcMain
?
const electron = require('electron')
const {ipcMain} = require('electron')
它们似乎都分配了 electron
模块的内容,但它们的功能显然有所不同。
They both appear to assign the contents of the electron
module, but they obviously function differently.
有人可以阐明吗?
推荐答案
第二个示例使用解构。
这将调用特定的变量(包括函数)从所需的模块导出。
This will call the specific variable (including functions) that are exported from the required module.
例如(functions.js):
For example (functions.js):
module.exports = {
func1,
func2
}
包含在您的文件中:
const { func1, func2 } = require('./functions')
现在您可以n分别称呼他们,
Now you can call them individually,
func1()
func2()
而不是:
const Functions = require('./functions')
使用点表示法调用:
Functions.func1()
Functions.func2()
希望这会有所帮助。
您可以阅读有关解构此处,它是ES6的非常有用的一部分,可以与数组以及对象一起使用。
You can read about destructuring here, it is a very useful part of ES6 and can be used with arrays as well as objects.
这篇关于Node.js'require'语句中的花括号(大括号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!