你真的永远都不用var了吗?

javascript的一些争论已经浮现出了一些经典的案例,因此,es6的拥护者你们应该讲var遗忘吗?这篇博客将带你走进被遗忘的角落

首先举例反对者的几个观点:

1、如果使用var 相同变量定义两次不会报错,当然,如果let和const会报错。

2、和let、const相比,var 不是块级作用域(他们是函数作用域function scoped),这是你不使用他的一个很重要的一个观点,因此如果使用var就需要在函数作用域的环境中使用,这个函数并且在全局作用域中,我没能够相处这个绝对的场景,也许它存在!

3、ESlint阻止使用var

4、大多数专家同意var不应该被使用,那个推广JSON的Douglas Crockford 表示:‘var在极端的例子里也许是有用的’,但Wes Bos仍然说:‘他不会使用var’

翻译原文:

Should You Truly Never Use var?

Some controversy in the Javascript community has emerged over this classic variable declaration. So, are the E6 purists correct and we should forever delegate var to the dustbin? This blog post will get to the bottom of the debate.

First, the arguments against var (the first two adopted from Learn Verified’s new and excellent V3 curriculum).

1. No error is thrown if you declare the same variable twice using var (conversely, both let and const will throw an error if a variable is declared twice)

2. Variables declared with var are not block scoped (although they are function scoped), while with let and const they are. This is important because what’s the point of block scoping if you’re not going to use it. So using var in this context would require a situation in which a variable declared inside a function would need to be used in the global scope. I’m not able to think of any situations where that would be absolutely necessary, but perhaps they exist.

3. Another argument that has been made against the use of var is that JS Linters are now pointing out their use as bad practice. I conducted a quick search on the Internet revealed that Jslint.com and Jshint.com, and did not raise an issues with the use of var as a variable declaration. However, ESLint explicitly has a no-var rule aimed at discouraging the use of var.

4. Most Javascript experts agree var shouldn’t be used. Douglas Crockford, the man who popularized JSON, is against the use of var. He indicates that, “var might possibly still be useful in an extreme case like machine-generated code, but I’m stretching hard there.” Wes Bos also says he won’t use var.

原文链接:https://medium.com/@johnwolfe820/should-you-truly-never-use-var-9214611ab962

作者:John Wolfe

var let const的更多相关文章

  1. let、var、const声明的区别

    前言 看了方应杭老师的一篇解释let的文章,对JavaScript中的声明有了深刻的理解,这里也就有了总结一下JavaScript中各种声明之间区别的这篇文章. JavaScript中变量声明机制 首 ...

  2. var let const 的区别

    Var let const 的区别 1.Var 定义的变量存在变量提升,而了let和const不存在变量提升.即在定义的变量代码上使用该变量,var的会输出undefined,而let的会报错. 2. ...

  3. ES6和ES5变量声明的区别(var let const)

    // es5的语法与es6的语法区别 // var let const console.log(name);//undefine,不会报错,因为变量声明会提到作用域的最前面 var name=&quo ...

  4. var和const和let的区别

    简述: 1.前端的变量申明,可以用到var,ES6的const(衡量)/let(变量) 2.在ES5用的都是var,到ES6之后,也就是2015年开始出现const/let. var 不会报错,有声明 ...

  5. let、var、const区别(表格比较)

    let.var.const区别(表格比较): 区别项 let var const 作用域 块级作用域 全局作用域或函数作用域 块级作用域 是否有变量提升 无 有 无 是否可重复声明 不可 可以 不可 ...

  6. var let const的一些区别

    var let const 都是来定义变量的. var let 作用域有些区别. const 类似于java中的常量的概念.即:只能给一个变量赋值一次,即指定一个引用. 举例来说: function ...

  7. javascript中var let const三种变量声明方式

    javascript中var let const三种变量声明方式 1.var  ①var表示声明了一个变量,并且可以同时初始化该变量. ②使用var语句声明的变量的作用域是当前执行位置的上下文:一个函 ...

  8. es6(var,let,const,set,map,Array.from())

    1.变量声明--var,const,let 1.1 var - (全局作用域,局部作用域)会有变量提升 //第一个小例子 <script> var num = 123; function ...

  9. let、var、const用法区别

    1.var var 声明的变量为全局变量,并会进行变量提升:也可以只声明变量而不进行赋值,输出为undefined,以下写法都是合法的. var a var a = 123  2.let let 声明 ...

  10. 浅谈JS中 var let const 变量声明

    浅谈JS中 var let const 变量声明 用var来声明变量会出现的问题: 1. 允许重复的变量声明:导致数据被覆盖 2. 变量提升:怪异的数据访问.闭包问题 3. 全局变量挂载到全局对象:全 ...

随机推荐

  1. kettle并行运行时出现「Unknown error in KarafBlueprintWatcher」

    背景:在使用kettle 6进行大量数据并行抽取时,偶尔会出现「Unknown error in KarafBlueprintWatcher」的错误,详细的报错信息可以查看下面的代码块. ERROR: ...

  2. Vue双向数据绑定原理

    https://www.cnblogs.com/kidney/p/6052935.html?utm_source=gold_browser_extension

  3. Wpf TemplateBinding

    TemplateBinding. ControlTemplate最终会被用到一个控件上,我们称这个控件为模板目标控件或者模板化控件,ControlTemplate里面的控件可以使用TemplateBi ...

  4. centos6.5-VMware虚拟机-双网卡绑定

    1 添加多张网卡(生产环境中有多个卡槽,可用ifconfig查看) 2 编辑两张虚拟机的网卡和物理机的连接方式,这里两张都使用NAT即可 3 打开虚拟机查看所有的网卡(网络接口),修改配置网卡配置文件 ...

  5. robotframework中的用evaluate关键字进行运算(随机数+转换+运算)

    当我们在写rf测试用例时,可能需要随机产生一些数据,可能需要将已有的数据进行转换,做简单的运算等:此时我们可以用万能的evaluate来实现 ,后面一般均适用python表达式来进行实现. 接下来详细 ...

  6. 【Jest】笔记三:全局变量

    一.前提 我们在使用unittest,testng框架的时候都知道,每个case都是项目独立的,上一个case返回的值是不能使用到下一个case的,但是实际中接口之间的关系是紧密相连的,这个时候我们怎 ...

  7. 2016第七届蓝桥杯C/C++语言A组

    一:问题: 某君新认识一网友.当问及年龄时,他的网友说:“我的年龄是个2位数,我比儿子大27岁,如果把我的年龄的两位数字交换位置,刚好就是我儿子的年龄” 请你计算:网友的年龄一共有多少种可能情况? 提 ...

  8. svn的分支与合并

    作者:fbysss msn:jameslastchina@hotmail.com  blog:blog.csdn.net/fbysss 声明:本文由fbysss原创,转载请注明出处 关键字:svn分支 ...

  9. css颜色的设置

    css的颜色设置 1.英文命令颜色 p{color:blue;}RGB颜色 2.与 photoshop 中的 RGB 颜色一致,由 R(red).G(green).B(blue) 三种颜色的比例来配色 ...

  10. javaEE十三种核心技术

    郭晨 软件151 1531610114 java的大方向就是JavaEE,JavaEE不仅仅是socket编程,具体包括13中核心技术. JAVAEE的核心API与组件 JAVAEE平台由一整套服务( ...