有没有理由不放弃“var”?

编程入门 行业动态 更新时间:2024-10-23 04:48:02
本文介绍了有没有理由不放弃“var”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在学习JavaScript的过程中,我了解到让和 const 被引入以解决 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”?

本文发布于:2023-11-29 08:29:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645870.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有理由   不放弃   var

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!