JavaScript の const と const {} の違いは何ですか? 質問する

JavaScript の const と const {} の違いは何ですか? 質問する

勉強するとき電子、BrowserWindow オブジェクトを取得する 2 つの方法を見つけました。

const {BrowserWindow} = require('electron')

そして

const electron = require('electron')
const BrowserWindow = electron.BrowserWindow

JavaScript のconstとの違いは何ですか?const {}

const {}なぜこれが機能するのか理解できません。JavaScript について何か重要なことを見逃しているのでしょうか?

ベストアンサー1

2つのコードは同等ですが、最初のコードはES6 分割代入短くなります。

仕組みの簡単な例を以下に示します。

const obj = {
  name: "Fred",
  age: 42,
  id: 1
}

//simple destructuring
const { name } = obj;
console.log("name", name);

//assigning multiple variables at one time
const { age, id } = obj;
console.log("age", age);
console.log("id", id);

//using different names for the properties
const { name: personName } = obj;
console.log("personName", personName);

おすすめ記事