Is there any reason not to abandon “var”?

送分小仙女□ 提交于 2019-11-28 03:47:45

问题


In the process of learning JavaScript I learned that Let and const were introduced to fix the problems of Var regarding the global scope and hoisting and not giving an error if re-declared.

Now can I write the code completely without using var ? or should I know about them for now and wait till they becomes widely "acceptable"?

In other words, for the time being should I be worried about compatibility issues if I only used let and const?


回答1:


To answer the question directly - no, you can't, because of the compatibility issues, as @suraj kindly reminded us.

Having said that, in the modern JS development you are increasingly unlikely to use var as let and const have clear advantages, apart from some specific uses of var and will use either BabelJS, TypeScript or even now Webpack 2 to transpile the code for backward compatibility as the production code will be shipped in vanilla JS. Modern IDEs, like WebStorm, will even lint your var's by default in ES6 mode to change them to let.

Judging from the question, you are still in the process of studying JS and you are already asking the right questions, so I would recommend to use today let and const to utilize the advantages of ES6, but that would add the complexity of dealing with the transpilers. But if you are OK with that - this is the way to go.



来源:https://stackoverflow.com/questions/43207177/is-there-any-reason-not-to-abandon-var

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!