2019-06-04
1298
#vue
Jake Dohm
2176
Jun 4, 2019 â‹… 4 min read

Cleaning up your Vue.js code with ES6+

Jake Dohm

Recent posts:

Comparing Mutative Vs Immer Vs Reducers For Data Handling In React

Comparing React state tools: Mutative vs. Immer vs. reducers

Mutative processes data with better performance than both Immer and native reducers. Let’s compare these data handling options in React.

Rashedul Alam
Apr 26, 2024 â‹… 7 min read
Radix Ui Adoption Guide Overview Examples And Alternatives

Radix UI adoption guide: Overview, examples, and alternatives

Radix UI is quickly rising in popularity and has become an excellent go-to solution for building modern design systems and websites.

Nefe Emadamerho-Atori
Apr 25, 2024 â‹… 11 min read
Understanding The Css Revert Layer Keyword, Part Of Css Cascade Layers

Understanding the CSS revert-layer keyword

In this article, we’ll explore CSS cascade layers — and, specifically, the revert-layer keyword — to help you refine your styling strategy.

Chimezie Innocent
Apr 24, 2024 â‹… 6 min read
Exploring Nushell, A Rust Powered, Cross Platform Shell

Exploring Nushell, a Rust-powered, cross-platform shell

Nushell is a modern, performant, extensible shell built with Rust. Explore its pros, cons, and how to install and get started with it.

Oduah Chigozie
Apr 23, 2024 â‹… 6 min read
View all posts

6 Replies to "Cleaning up your Vue.js code with ES6+"

  1. Great tips, one question, when I use the arrow functions I get undefined on this… Any suggestion?

    1. Hi, Maximiliano, thanks for reading!

      So, if “this” is returning “undefined”, you’re probably using arrow functions in the wrong place. You shouldn’t use them when defining a function for your data, or lifecycle methods, as you do want this to be bound to the context. So doing { mounted: () => { console.log(this.hello) } } will console log undefined (rightly).

      You should use arrow functions _within_ your methods, lifecylcles, etc. so that the context of “this” will always be your component.

      For more information on arrow functions, check out this article: https://codeburst.io/javascript-arrow-functions-for-beginners-926947fc0cdc

  2. (hello = 0) Destructuring works let { hello } = this and then I hello = 1 in the method and the value changes if I assign it to a different value but when a different method later calls the data() for that particular data it is as-if it was never changed at all because when I check it again it is still hello = 0.

    1. Hi, Lou!

      So the reason mutating the value of “hello” doesn’t work, is that primitive values (like a number or string) are copied by value, not reference. When you do let { hello } = this, what it’s really doing is let hello = this.hello, which *copies* the value of this.hello into a new local variable. So when you mutate your local variable, it won’t change the value of this.hello.

      For more information, check out this AWESOME article on value vs reference in JS: https://codeburst.io/explaining-value-vs-reference-in-javascript-647a975e12a0

Leave a Reply