loading...
Cover image for Property shorthands

Property shorthands

codingunicorn profile image Coding Unicorn šŸ¦„ 惻1 min read

Property shorthands

āŒ If you see code like this:

new Vuex.Store({
  modules: {
    creation: creation,
    fetching: fetching,
    authentication: authentication
  }
});

āœ… You can always refactor it to:

new Vuex.Store({
  modules: {
    creation,
    fetching,
    authentication
  }
});

šŸ§  If you want to define an object who's keys have the same name as the variables passed-in as properties, you can simply pass the key name. This feature is called "Object Property Value Shorthand".

āœ‹ But sometimes names don't match:

return { code: topicCode, name: topics[topicCode].name };

āœ… To simplify object creation with shorthands, simply extract variables and give them names that match object keys:

const code = topicCode;
const name = topics[code].name;
return { code, name };

šŸ’” With property shorthands, you can remove unnecessary noise from object definition. In practice, it comes down to renaming and creating variables to make their names match the object keys.


Every day, I post coding tips on my Instagram. Let's stay connected! šŸ¦„

Posted on by:

codingunicorn profile

Coding Unicorn šŸ¦„

@codingunicorn

Hi!āœŒ(ā—•ā€æ-)āœŒ My name is Julia (also known as coding_unicorn on Instagram). I'm a full-stack developer specializing in Java and JavaScript.

Discussion

markdown guide