admin管理员组

文章数量:1290960

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?

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?

Share Improve this question edited Jul 14, 2022 at 1:45 tony19 139k23 gold badges277 silver badges347 bronze badges asked Mar 13, 2019 at 22:22 leanderleander 6741 gold badge6 silver badges19 bronze badges 2
  • I would assume that its because that you would need an ES6 patible piler for the interpreter to handle those things. Vue wants to get people working JS straight in without necessary setup. Whether this is what they intended is another story. – Olufemi Adesina Commented Mar 13, 2019 at 22:27
  • doesnt Vue leverage babel and if so, doesnt it get converted to the correct syntax anway – Flame Commented Mar 13, 2019 at 22:30
Add a ment  | 

2 Answers 2

Reset to default 13

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

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


Use whatever you...

  1. Feel fortable 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

Besides the lowest mon 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.

本文标签: javascriptWhy use the var keyword in VuejsStack Overflow