微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript中const和const {}之间的区别是什么?

当我研究电子时,我找到了2种获取browserWindow对象的方法.

const {browserWindow} = require('electron')
@H_404_6@

const electron = require('electron')
const browserWindow = electron.browserWindow
@H_404_6@

JavaScript中的const和const {}有什么区别?

我无法理解为什么const {}可以工作.我是否想念JS的重要内容

解决方法:

这两段代码是等价的,但第一段是使用ES6 destructuring assignment更短.

以下是它如何工作的简单示例:

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);@H_404_6@

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐