有什么理由不放弃“var"吗?

2024-04-20前端开发问题
2

本文介绍了有什么理由不放弃“var"吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在学习 JavaScript 的过程中,了解到引入 Letconst 是为了解决 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"?

也就是说,如果我暂时只使用letconst,我应该担心兼容性问题吗?

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,因为 letconst 具有明显的优势,除了var 的一些特定用途,将使用 BabelJSTypeScript 甚至现在的 Webpack 2 来转换代码向后兼容性,因为生产代码将在 vanilla JS 中提供.现代 IDE,比如 WebStorm,甚至会默认在 ES6 模式下对你的 var 进行 lint,以将它们更改为 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,你已经问对了问题,所以我今天推荐使用letconst利用 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"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui树状组件tree怎么默认勾选?
在layui树状组件tree中,勾选问题可以通过以下方法解决: 通过tree的oncheck事件来监听勾选操作,然后根据勾选状态进行相应的处理。例如: tree.on('check', function(obj) { // 获取勾选状态 var isChecked = obj.checked; // 获取当前节点数据 var data =...
2024-11-09 前端开发问题
372

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125