zoukankan      html  css  js  c++  java
  • var let const

    你真的永远都不用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

  • 相关阅读:
    Redis面试题
    spring boot错误: 找不到或无法加载主类
    JAVA的高并发编程
    Redis多机多节点集群实验
    Redis单机多节点集群实验
    Redis集群概述
    Redis的持久化之AOF方式
    Redis的持久化之RDB方式
    Redis持久化介绍
    Redis Keys的通用操作
  • 原文地址:https://www.cnblogs.com/fooller/p/8406310.html
Copyright © 2011-2022 走看看