1. 函数参数默认值

不使用ES6

为函数的参数设置默认值:

function foo(height, color)
{
var height = height || 50;
var color = color || 'red';
//...
}

这样写一般没问题,但是,当参数的布尔值为false时,是会出事情的!比如,我们这样调用foo函数:

foo(0, "", "")//因为0的布尔值为false,这样height的取值将是50。同理color的取值为‘red’。

使用ES6

function foo(height = 50, color = 'red')
{
// ...
}

2. 模板字符串

不使用ES6

使用+号将变量拼接为字符串:

var name = 'Your name is ' + first + ' ' + last + '.' 

使用ES6

将变量放在大括号之中:

var name = `Your name is ${first} ${last}.`;

ES6的写法更加简洁、直观。

3. 多行字符串

不使用ES6

使用“\n\t”将多行字符串拼接起来:

var roadPoem = 'Then took the other, as just as fair,\n\t'
+ 'And having perhaps the better claim\n\t'
+ 'Because it was grassy and wanted wear,\n\t'
+ 'Though as for that the passing there\n\t'
+ 'Had worn them really about the same,\n\t'

使用ES6

将多行字符串放在反引号“之间就好了:

var roadPoem = `Then took the other, as just as fair,
And having perhaps the better claim
Because it was grassy and wanted wear,
Though as for that the passing there
Had worn them really about the same,`

4. 解构赋值

不使用ES6

当需要获取某个对象的属性值时,需要单独获取:

var data = $('body').data(); // data有house和mouse属性
var house = data.house;
var mouse = data.mouse;

使用ES6

一次性获取对象的子属性:

var { house, mouse} = $('body').data()

对于数组也是一样的:

var [col1, col2]  = $('.column');

5. 对象属性简写

不使用ES6

对象中必须包含属性和值,显得非常多余:

var bar = 'bar';
var foo = function ()
{
// ...
}
var baz = {
bar: bar,
foo: foo
};

使用ES6

对象中直接写变量,非常简单:

var bar = 'bar';
var foo = function ()
{
// ...
}
var baz = { bar, foo };

  

 

 

 

最新文章

  1. PostgreSQL Replication之第十章 配置Slony(5)
  2. sql case when 多条件
  3. 利用Python获取ZOJ所有题目的名字
  4. ASP读取RSS
  5. mysqldump导出csv格式文件
  6. 纯css实现slide效果
  7. 减少iOS应用程序安装包大小
  8. Android便携式热点的开启状态检测和SSID的获取
  9. PHP文件操作整理
  10. bzoj5250 [2018多省省队联测]秘密袭击
  11. scrollTo不起作用
  12. strace -> System call tracer
  13. Linux和windows 平台下启动和关闭mysql服务
  14. 移动端video标签默认置顶的解决方案
  15. Hibernate中的Entity类之间的继承关系之一MappedSuperclass
  16. Error:Failed to resolve: :Base:
  17. 分析网络流量Capsa笔记
  18. zabbix 监控Nginx和PHP
  19. Source InSight context 窗口丢失的解决办法
  20. [javase学习笔记]-8.1 statickeyword之特点

热门文章

  1. sysbench安装
  2. jQuery实现 自动滚屏操作
  3. 【mongdb主从复制和同步】
  4. HTML5中的拖拽与拖放(drag&&drop)
  5. json传值给前端页面,出现堆栈溢出问题
  6. 单片机-C语言-定义和申明
  7. 结合《需求征集系统》谈MVC框架
  8. 优步UBER司机全国各地奖励政策汇总 (3月7日-3月13日)
  9. 北京Uber优步司机奖励政策(12月10日)
  10. 《C++ Primer》第II部分:C++标准库