热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

有什么理由我不会用“让”吗?[重复]-IsthereanyreasonIwouldn'tuse“let”?[duplicate]

Thisquestionalreadyhasananswerhere:这个问题在这里已有答案:WhatistheusecaseforvarinES

This question already has an answer here:

这个问题在这里已有答案:

  • What is the use case for var in ES6? 4 answers
  • ES6中var的用例是什么? 4个答案

Now that we can write ES6 and deploy it to browsers (using Traceur or 6to5 to support legacy user agents), is there any reason why we wouldn't use let or const as our default keywords for variable declaration?

现在我们可以编写ES6并将其部署到浏览器(使用Traceur或6to5来支持传统的用户代理),我们有什么理由不使用let或const作为变量声明的默认关键字吗?

Is var dead? And if it is, can I configure my linting tools to reject it?

var死了吗?如果是,我可以配置我的linting工具来拒绝吗?

3 个解决方案

#1


As es5 only has function scope for variables, presumably your transpiler is creating closures in order to mock the let keyword's functionality. That may have an effect on the resulting size of your code if you're declaring variables inside scopes that are not functions (e.g. loops, ifs etc). So that is one reason not to, currently. It may also make debugging slightly more confusing, although this could be mitigated using sourcemaps.

因为es5只有变量的函数范围,所以你的转换器可能是为了模拟let关键字的功能而创建闭包。如果您在范围内声明非函数的变量(例如循环,ifs等),则可能会对代码的结果大小产生影响。所以这是目前没有的一个原因。它也可能使调试稍微混乱,尽管可以使用源映射来减轻这种情况。

There aren't really any other drawbacks. Variable hoisting is pretty confusing to people coming from other languages, and using let allows you to avoid this potential hiccup. So I would use it now if you can.

实际上没有任何其他缺点。对于来自其他语言的人来说,可变提升非常令人困惑,使用let可以避免这种潜在的打嗝。如果可以,我现在就用它。

#2


It depends on your environment :

这取决于你的环境:

  • If you're writing server-side Javascript code (Node.js), you can safely use the let statement.

    如果您正在编写服务器端Javascript代码(Node.js),则可以安全地使用let语句。

  • If you're writing client-side Javascript code and use a transpiler like Traceur, you can can safely use the let statement, however your code is likely to be anything but optimal with respect to performance.

    如果您正在编写客户端Javascript代码并使用像Traceur这样的转换器,您可以安全地使用let语句,但是您的代码在性能方面可能不是最优的。

  • If you're writing client-side Javascript code and don't use a transpiler, you need to consider browser support.

    如果您正在编写客户端Javascript代码而不使用转换器,则需要考虑浏览器支持。

    Today, Feb 23 2016, these are some browsers that either don't support let or have only partial support :

    今天,2016年2月23日,这些是一些不支持let或只有部分支持的浏览器:

    • Internet explorer 10 and below (no support)
    • Internet Explorer 10及以下版本(不支持)

    • Firefox 43 and below (no support)
    • Firefox 43及以下版本(不支持)

    • Safari 9 and below (no support)
    • Safari 9及以下版本(不支持)

    • Opera Mini 8 and below (no support)
    • Opera Mini 8及以下版本(不支持)

    • Android browser 4 and below (no support)
    • Android浏览器4及以下(不支持)

    • Opera 36 and below (partial support)
    • Opera 36及以下(部分支持)

    • Chome 51 and below (partial support)
    • Chome 51及以下(部分支持)

For an up-to-date overview of which browsers support the let statement at the time of your reading this answer, see this Can I Use page.

有关哪些浏览器在您阅读此答案时支持let语句的最新概述,请参阅此“可以使用”页面。

#3


At times, let doesn't allow the named function hoisting pattern. It does in Chrome 51, but does not in Safari 9.1.1. Thus,

有时,让我们不允许命名函数提升模式。它在Chrome 51中有效,但在Safari 9.1.1中没有。从而,

let foo = func;

function func () {
    return true;
}

would throw an error. Whereas the following style is valid:

会抛出错误。以下样式有效:

var foo = func;

function func () {
    return true;
}

Depending on your purposes, this could make your code more or less readable. For example, an exposing module pattern which has a lot of initialization code, so doesn't return immediately.

根据您的目的,这可能会使您的代码具有或多或少的可读性。例如,曝光模块模式具有大量初始化代码,因此不会立即返回。

var foo = (function () {
    let factory = {
        func: func
    };

    init();
    // do more init stuff

    return factory;

    function func () {
        //
    }
}());

Using function declarations to hide implementation details, for example.

例如,使用函数声明来隐藏实现细节。


推荐阅读
author-avatar
鄙人嘘嘘_594
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有