4
I recently saw a type of Javascript code, in some examples of Electron, which I believe is part of ES6. These are variable statements in the following format:
const electron = require('electron');
// Module to control application life.
const {app} = electron;
// Module to create native browser window.
const {BrowserWindow} = electron;
Here is being used with const
, but I don’t know if this also exists with let
and var
.
The important thing here is that instead of declaring in the usual way
const app = electron;
const BrowserWindow = electron;
We’re putting app
and BrowserWindow
between keys.
What does this type of declaration mean with the variable name inside keys? When and how should this type of declaration be used?
Sérgio already answered your question, but if you want to declare these keys to get several properties of an object it would be better to declare the
with
instead. Example:var obj = {prop: 5, ah: 2, uuh: true, y: false}; with(obj) { alert(prop); }
. Thewith
expands an object and is supported from ES1.– Klaider