首页 > 技术文章 > ES6新特性学习

knyel 2017-11-13 14:43 原文

ES6相比于ES5有很多的好用的新特性,这里我总结一些,但是还有很多特性待以后完善

详细资料见:ES6 API

1.默认参数

1)ES5的实现方式

 

function test(txt) {
    txt = txt || 'hello world'
}

2)ES6的实现方式

 

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Examples</title>
<script type="text/javascript" src="js/index.js"></script>
<script>
    function test(txt='hello world'){
    console.log(txt)
}
test('nihao')
test()
</script>
</head>
<body>
    
</body>
</html>

打印出的结果如下

nihao
hello world

2.字符串模版

1)ES5的实现(依赖第三方的库underscore.js)

var cmpiled = _.template("hello <%= name %>");
compiled.({name: knyel}); // "hello knyel"

参考资料:Underscore模版引擎的使用-template方法

2)ES6的实现(不依赖第三方库)

var name='knyel'
var txt=`hello ${name}`

结果为

hello knyel

3.解构赋值

4.箭头函数

5.数据结构 Set和Map

6.异步操作

比如Promise

7.类与对象

8.模块化

 

推荐阅读