{app,BrowserWindow}在JavaScript(node.js)中是什么意思? [英] What does {app, BrowserWindow} means in JavaScript (node.js)?

查看:245
本文介绍了{app,BrowserWindow}在JavaScript(node.js)中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读使用 electron 制作软件的文档时,我在<$开头遇到了这类代码c $ c> index.js 文件(通常开始执行的文件)

While reading docs of making softwares with electron, I came across this type of code in the beginning of index.js file (the file where generally execution starts)

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

{app, BrowserWindow} (语法,而不是关键字)的真正含义是?是JavaScript语法,还是node.js事物,还是与电子专有相关的事物?

What does {app, BrowserWindow} (the syntax, not the keywords) really means? Is it a JavaScript syntax, or a node.js thing or something exclusively related to electron?

推荐答案

这种语法称为对象解构,这是最新版本的JavaScript(JavaScript2015 aka ECMAScript 6 / ES6)的功能- app BrowserWindow 只是您要在应用程序的此部分中使用的电子的特定部分。

This syntax is called 'object destructuring', and it is a feature of the latest version of JavaScript (JavaScript2015 aka ECMAScript 6/ES6) - app and BrowserWindow are just particular parts of electron that you want to use in this portion of your application.

这是一个简化代码并轻松引用依赖项的关键部分的方法。

It's a way to simplify your code and to easily reference critical parts of a dependency.

这是 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

var o = {p: 42, q: true};
var {p, q} = o;

console.log(p); // 42
console.log(q); // true

因此,在您的情况下, electron 是一个导入的模块,看起来像这样(再次,这里过于简化):

So in your case, electron is an imported module that would look something like (again, a gross oversimplification here):

var electron = {
    app: {
        greet: () => {
            console.log("Hello, world!")
        }
    },
    BrowserWindow: {/* some other stuff */},
    anotherMethod: {/* other stuff, which we will ignore in your app */}
}

module.exports electron

然后在您的应用程序中,导入此模块,然后可以直接引用导入的属性:

Then in your app, you import this module and you can reference the imported attributes directly:

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

app.greet()
// "Hello, world!"

类似地,您可以引用 BrowserWindow 。 ..但是,您不能在未将 otherMethod 包含在解构分配中的情况下对其进行引用。

And similarly, you can reference BrowserWindow... however, you couldn't reference anotherMethod without including it in the destructuring assignment.

希望有帮助。

这篇关于{app,BrowserWindow}在JavaScript(node.js)中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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