节点中的括号(括号)需要声明 [英] Curly brackets (braces) in node require statement
问题描述
我试图理解下面两个require语句之间的区别。
I am trying to understand the difference between the two require statements below.
具体来说, {}的目的是什么
s缠绕 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?
推荐答案
第二个例子使用解构。
这将调用特定的变量(包括函数)从所需模块导出。
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')
现在你可以单独打电话了,
Now you can call them individually,
func1()
func2()
而不是:
const Functions = require('./functions')
使用点表示法调用:
Functions.func1()
Functions.func2()
希望这会有所帮助。
您可以阅读有关解构< a href =https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment>这里,它是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.
这篇关于节点中的括号(括号)需要声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!