Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use the `var` keyword in Vue.js?

Every tutorial and code snippet I'm looking at while learning the framework all use var for their declarations, including the official docs.

Preface, I'm just starting to learn Vue, so I know very little about it, but haven't found an answer yet.

Same with other like assuming property name:

new Vue({
  data: data
})

vs.

new Vue({
  data
})

Am I wrong in assuming that ES6's const and let should be standard? Is there a reason to use var for Vue.js? Is there an issue with ES6?

like image 794
Leander Rodrigues Avatar asked Mar 13 '19 22:03

Leander Rodrigues


2 Answers

Why do the docs use var and avoid ES6 features? I'd say to support lowest common denominator, ie, worst browser.

Since Vue can be included as a plain old <script> tag (UMD / global, no build system) and supports all ES5-compliant browsers (IE9+), they keep the documentation consistent.


Use whatever you...

  1. Feel comfortable using, and
  2. Is supported by the target production environment
    • your build system (if you're using one) can help transpile ES6 code to a lower language level
like image 124
Phil Avatar answered Oct 19 '22 22:10

Phil


Besides the lowest common denominator arguments I would like to point that var and let have different semantics.

When using var variables are function scoped and they get hoisted. When using let they are blocked scoped and they don't get hoisted.

So even if let and const are standard they (probably) won't replace var any time soon.

like image 2
Radu Diță Avatar answered Oct 19 '22 22:10

Radu Diță