Home > Enterprise >  Javascript const object syntax - what is this doing? (using in a Vue script)
Javascript const object syntax - what is this doing? (using in a Vue script)

Time:04-03

What is the following statement doing, and why use const vs var?

const { SearchIcon } = myApp.icons;

For context, I am learning Vue and new to Javascript. I saw this in an example. The SearchIcon is an icon that is being imported from heroicons, MyApp.icons is being defined in a different script file like this:

window.MyApp = {
    app: null,
    icons: {},
...

CodePudding user response:

Looks like your issue is that you're storing MyApp under window, but then trying to access it directly. You've also got a capital M in the definition, and a lowercase when your accessing it.

Here's a working example:

window.MyApp = {
  app: null,
  icons: {
    SearchIcon : 'Hello!'
  },
};

const { SearchIcon } = window.MyApp.icons;

console.log(SearchIcon);

For more info, see the docs on object destructuring.

Hope that helps :)

  • Related