es6${}、for循环for-of

Template Literals(模板对象) in ES6

如在es5中拼凑字符串需要+''以及变量名

var name = 'Your name is ' + first + ' ' + last + '.';
var url = 'http://localhost:3000/api/messages/' + id;

在es6中新增添了${}的使用方法

var name = `Your name is ${first} ${last}. `;
var url = `http://localhost:3000/api/messages/${id}`;
多行字符串

在es6中多行字符串也不需要进行拼接了,直接写入就好
es5中

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,`
for循环

在es5中for循环包含了基础for循环,for-in,for-each

基础for循环

for(var i = 1;i<=5;i++0{
console.log(i);
}
书写麻烦

for-in

var array = [1,2,3,4,5];
for(var index in array){
console.log(array[index]);
}
效率极低,为其他循环的1/7

for-each

const arr = [1, 2, 3];
arr.forEach((data) => {
console.log(data);
});
forEach 不能 break 和 return;
for-in 缺点更加明显,它不仅遍历数组中的元素,还会遍历自定义的属性,甚至原型链上的属性都被访问到。而且,遍历数组元素的顺序可能是随机的。

for-of

const arr = [‘a‘, ‘b‘, ‘c‘];
for(let data of arr) {
console.log(data);
}
总结一下,for-of 循环有以下几个特征:

这是最简洁、最直接的遍历数组元素的语法。
这个方法避开了 for-in 循环的所有缺陷。
与 forEach 不同的是,它可以正确响应 break、continue 和 return 语句。
其不仅可以遍历数组,还可以遍历类数组对象和其他可迭代对象。

感谢 http://www.mamicode.com/info-detail-1732094.html

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第2章 基本语法 2.1 概述 基本句法和变量 语句 JavaScript程序的执行单位为行(line),也就是一...
    悟名先生阅读 4,195评论 0 13
  • 在ECMAScript5(简称 ES5)中,有三种 for 循环,分别是: 简单for循环 for-in forE...
    l4u阅读 574评论 0 0
  • 1、新的声明方式 以前我们在声明时只有一种方法,就是使用var来进行声明,ES6对声明的进行了扩展,现在可以有三种...
    令武阅读 1,017评论 0 7
  • 我们在学习web前端的路程起步时总是疑问,我们如何更好的遍历元素呢?迭代器和生成器是什么?今天为大家带上与精彩的E...
    侬姝沁儿阅读 3,368评论 0 6
  • 立冬 2016-11-10一直等着没下手 总是规划了好多,还没来得及实现,就已立冬了。 冬日,总是习惯沉睡。感受凛...
    实在想不出昵称丶阅读 188评论 0 0