what is the difference between const and const {} in javascript

前端 未结 3 1031
挽巷
挽巷 2020-11-28 20:06

When I study electron, I found 2 ways of getting BrowserWindow object.

const {BrowserWindow} = require(\'electron\')

and

co         


        
3条回答
  •  醉酒成梦
    2020-11-28 20:19

    const {BrowserWindow} = require('electron')
    

    Above syntax uses ES6. If you have an object defined as:

    const obj = {
        email: "hello@gmail.com",
        title: "Hello world"
    }
    

    Now if we want to assign or use email and title field of obj then we don't have to write the whole syntax like

    const email = obj.email;
    const title = obj.title;
    

    This is old school now.

    We can use ES6 Destructuring assignment i.e., if our object contains 20 fields in obj object then we just have to write names of those fields which we want to use like this:

    const { email,title } = obj;
    

    This is ES6 syntax-simpler one It will automatically assign email and title from obj, just name has to be correctly stated for required field.

提交回复
热议问题