Node.js“require"语句中的大括号(大括号) [英] Curly brackets (braces) in Node.js 'require' statement

查看:45
本文介绍了Node.js“require"语句中的大括号(大括号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解下面两个要求"语句之间的区别.

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

具体来说,包裹在 ipcMain 周围的 { } 的目的是什么?

Specifically, what is the purpose of the { }s wrapped around 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')

现在你可以单独调用它们了,

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆