有没有理由不放弃“var”? [英] Is there any reason not to abandon "var"?

查看:149
本文介绍了有没有理由不放弃“var”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习JavaScript的过程中,我了解到 const 被引入以解决<$的问题c $ c> Var 关于全局范围和提升,如果重新声明则不会出错。

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.

现在我可以完全编写代码不使用 var ?或者我现在应该知道它们并等到它们被广泛接受?

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"?

换句话说,暂时我应该担心兼容性问题如果我只使用 const

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

推荐答案

直接回答这个问题 - 不,你不能,因为兼容性问题,正如@suraj提醒我们的那样。

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

话虽如此,在现代JS开发中,你越来越不可能使用 var 作为和除了 var 的某些特定用途之外, const 具有明显的优势,并将使用 BabelJS TypeScript 甚至现在 Webpack 2 以转换代码以实现向后兼容,因为生产代码将在 vanilla JS中提供。像WebStorm这样的现代IDE甚至会在ES6模式下默认你的 var ,将它们更改为 let

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.

从这个问题来看,你仍然在研究JS,你已经在提出正确的问题,所以我建议今天使用 const 来利用ES6的优点,但这会增加处理转换器的复杂性。但如果你对此感到满意 - 这就是你要走的路。

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.

这篇关于有没有理由不放弃“var”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆