-2

On this page in the example code constants are defined with {}

https://github.com/electron/electron/blob/master/docs/api/net.md

const {app} = require('electron')

What does {name} do compared to just name?

Also, I tried the code above with jquery and I an error "cannot read property request of undefined".

jQuery(document).ready(function($){

  const {net} = require('electron');
  const request = net.request('https://github.com'); <- here
Elfy
  • 1,733
  • 6
  • 19
  • 39
  • It's destructuring assignment: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment – jonrsharpe Apr 09 '17 at 12:51
  • The example uses `app.on('ready')`, does it work if you use that? – dayuloli Apr 09 '17 at 12:57
  • no... I added console.logs for app and net and they appear undefined.. it's like the whole script can't see electron. The script is included inside index.html using require('./renderer.js') – Elfy Apr 09 '17 at 12:58
  • nvm found out why, i have to use the "remote" feature.. – Elfy Apr 09 '17 at 20:18

1 Answers1

1

The {} allow you to 'extract' the app property from the object you're requiring.

It is a feature of ECMAScript 2015 (a.k.a. ES6 or ES2015) called Destructuring assignment. The following two lines are equivalent:

const {app} = require('electron');
const app = require('electron').app;
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
dayuloli
  • 16,205
  • 16
  • 71
  • 126