本文介紹了JavaScript 中的 const 和 const {} 有什么區別的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
在學習electron的時候,發現了兩種獲取BrowserWindow對象的方法.
When I study electron, I found 2 ways of getting BrowserWindow object.
const {BrowserWindow} = require('electron')
和
const electron = require('electron')
const BrowserWindow = electron.BrowserWindow
JavaScript中的const
和const {}
有什么區別?
What is the difference between const
and const {}
in JavaScript?
我不明白為什么 const {}
可以工作.我錯過了關于 JS 的任何重要內容嗎?
I can't understand why the const {}
can work. Do I miss anything important about JS?
推薦答案
兩段代碼是等價的,但第一段使用的是 ES6 解構賦值 要更短.
The two pieces of code are equivalent but the first one is using the ES6 destructuring assignment to be shorter.
下面是一個簡單的例子來說明它是如何工作的:
Here is a quick example of how it works:
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);
這篇關于JavaScript 中的 const 和 const {} 有什么區別的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!