zoukankan      html  css  js  c++  java
  • ECMAScript6 入门教程记录之-编程风格

    1、块级作用于域

      1.let取代var (let没有副作用)

      2.全局常量和线程安全

    1.const可以提醒阅读程序的人,这个变量不应该改变;
    2.const比较符合函数式编程思想,运算不改变值,只是新建值,而且这样也有利于将来的分布式运算
    3. JavaScript 编译器会对const进行优化,所以多使用const,有利于提高程序的运行效率,也就是说let和const的本质区别,
    其实是编译器内部的处理不同。
    4.常量赋值
    // bad
    var a = 1, b = 2, c = 3;
    
    // good
    const a = 1;
    const b = 2;
    const c = 3;
    
    // best
    const [a, b, c] = [1, 2, 3];

    2、字符串

    静态字符串一律使用单引号或反引号,不使用双引号。动态字符串使用反引号。

      

    // bad
    const a = "foobar";
    const b = 'foo' + a + 'bar';
    
    // acceptable
    const c = `foobar`;
    
    // good
    const a = 'foobar';   //静态a
    const b = `foo${a}bar`;//变量b  $+静态值=

    3、变量的解构赋值

    使用数组成员对变量赋值时,优先使用解构赋值
    const arr = [1, 2, 3, 4];
    
    // bad
    const first = arr[0];
    const second = arr[1];
    
    // good
    const [first, second] = arr;

      函数的参数如果是对象的成员,优先使用解构赋值。

    // bad
    function getFullName(user) {
      const firstName = user.firstName;
      const lastName = user.lastName;
    }
    
    // good
    function getFullName(obj) {
      const { firstName, lastName } = obj;
    }
    
    // best
    function getFullName({ firstName, lastName }) {
    }

    本质上,这种写法属于“模式匹配”,只要等号两边的模式相同,左边的变量就会被赋予对应的值。

      tips:ES6 内部使用严格相等运算符(===),判断一个位置是否有值。所以,只有当一个数组成员严格等于undefined,默认值才会生效。

    4、对象

      单行定义的对象,最后一个成员不以逗号结尾。多行定义的对象,最后一个成员以逗号结尾。

    // bad
    const a = { k1: v1, k2: v2, };//单行定义对象
    const b = {
      k1: v1,
      k2: v2
    };//多行定义对象
    
    // good
    const a = { k1: v1, k2: v2 };
    const b = {
      k1: v1,
      k2: v2,
    };

    5、数组

      使用扩展运算符(...)拷贝数组。

     

    // bad
    const len = items.length;
    const itemsCopy = [];
    let i;
    
    for (i = 0; i < len; i++) {
      itemsCopy[i] = items[i];
    }
    
    // good
    const itemsCopy = [...items];

    6、函数

      立即执行函数可以写成箭头函数的形式。

     

    (() => {
      console.log('Welcome to the Internet.');
    })();

    使用匿名函数当作参数的场合,尽量用箭头函数代替。因为这样更简洁,而且绑定了 this。

    // bad
    [1, 2, 3].map(function (x) {
      return x * x;
    });
    
    // good
    [1, 2, 3].map((x) => {
      return x * x;
    });
    
    // best
    [1, 2, 3].map(x => x * x);

    箭头函数取代Function.prototype.bind,不应再用 self/_this/that 绑定 this。

    // bad
    const self = this;
    const boundMethod = function(...params) {
      return method.apply(self, params);
    }
    
    // acceptable
    const boundMethod = method.bind(this);
    
    // best
    const boundMethod = (...params) => method.apply(this, params);

    简单的、单行的、不会复用的函数,建议采用箭头函数。如果函数体较为复杂,行数较多,还是应该采用传统的函数写法。

  • 相关阅读:
    Android SDK 国内镜像及配置方法
    Python多线程学习
    JProfiler 8下载地址和注册码
    Python自动化测试工具Splinter简介和使用实例
    linux系统新建用户ssh远程登陆显示-bash-4.1$解决方法
    Linux系统安装VMware Tools
    CentOS minimal网络设置
    接口测试之webservice
    VirtualBox-Linux系统安装增强功能
    PhantomJS快速入门
  • 原文地址:https://www.cnblogs.com/cdj61/p/9517552.html
Copyright © 2011-2022 走看看