关于ES6的10个最佳特性

简介:

关于ES6的10个最佳特性


ES6,正式名称是ECMAScript2015,但是ES6这个名称更加简洁。ES6已经不再是JavaScript最新的标准,但是它已经广泛用于编程实践中。如果你还没用过ES6,现在还不算太晚...

下面是10个ES6最佳特性,排名不分先后:

  • 函数参数默认值
  • 模板字符串
  • 多行字符串
  • 解构赋值
  • 对象属性简写
  • 箭头函数
  • Promise
  • Let与Const
  • 模块化

1. 函数参数默认值

不使用ES6

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


 
 
  1. function foo(height, color) 
  2.     var height = height || 50; 
  3.     var color = color || 'red'
  4.     //... 
  5. }  

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


 
 
  1. foo(0, """"

因为0的布尔值为false,这样height的取值将是50。同理color的取值为'red'。

使用ES6


 
 
  1. function foo(height = 50, color = 'red'
  2.     // ... 
  3. }  

2. 模板字符串

不使用ES6

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


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

使用ES6

将变量放在大括号之中:


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

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

3. 多行字符串

不使用ES6

使用"nt"将多行字符串拼接起来:


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

使用ES6

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


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

4. 解构赋值

不使用ES6

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


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

使用ES6

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


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

对于数组也是一样的:


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

5. 对象属性简写

不使用ES6

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


 
 
  1. var bar = 'bar'
  2. var foo = function () 
  3.     // ... 
  4.  
  5. var baz = { 
  6.   bar: bar, 
  7.   foo: foo 
  8. };  

使用ES6

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


 
 
  1. var bar = 'bar'
  2. var foo = function () 
  3.     // ... 
  4.  
  5. var baz = { bar, foo };  

6. 箭头函数

不使用ES6

普通函数体内的this,指向调用时所在的对象。


 
 
  1. function foo()  
  2.     console.log(this.id); 
  3.  
  4. var id = 1; 
  5.  
  6. foo(); // 输出1 
  7.  
  8. foo.call({ id: 2 }); // 输出2  

使用ES6

箭头函数体内的this,就是定义时所在的对象,而不是调用时所在的对象。


 
 
  1. var foo = () => { 
  2.   console.log(this.id); 
  3.  
  4. var id = 1; 
  5.  
  6. foo(); // 输出1 
  7.  
  8. foo.call({ id: 2 }); // 输出1  

7. Promise

不使用ES6

嵌套两个setTimeout回调函数:


 
 
  1. setTimeout(function() 
  2.     console.log('Hello'); // 1秒后输出"Hello" 
  3.     setTimeout(function() 
  4.     { 
  5.         console.log('Fundebug'); // 2秒后输出"Fundebug" 
  6.     }, 1000); 
  7. }, 1000);  

使用ES6

使用两个then是异步编程串行化,避免了回调地狱:


 
 
  1. var wait1000 = new Promise(function(resolve, reject) 
  2.     setTimeout(resolve, 1000); 
  3. }); 
  4.  
  5. wait1000 
  6.     .then(function() 
  7.     { 
  8.         console.log("Hello"); // 1秒后输出"Hello" 
  9.         return wait1000; 
  10.     }) 
  11.     .then(function() 
  12.     { 
  13.         console.log("Fundebug"); // 2秒后输出"Fundebug" 
  14.     });  

8. Let与Const

使用Var

var定义的变量未函数级作用域:


 
 
  1.   var a = 10; 
  2.  
  3. console.log(a); // 输出10  

使用let与const

let定义的变量为块级作用域,因此会报错:(如果你希望实时监控JavaScript应用的错误,欢迎免费使用Fundebug)


 
 
  1.   let a = 10; 
  2.  
  3. console.log(a); // 报错“ReferenceError: a is not defined”  

const与let一样,也是块级作用域。

9. 类

不使用ES6

使用构造函数创建对象:


 
 
  1. function Point(x, y) 
  2.     this.x = x; 
  3.     this.y = y; 
  4.     this.add = function() 
  5.     { 
  6.         return this.x + this.y; 
  7.     }; 
  8.  
  9. var p = new Point(1, 2); 
  10.  
  11. console.log(p.add()); // 输出3  

使用ES6

使用Class定义类,更加规范,且你能够继承:


 
 
  1. class Point 
  2.     constructor(x, y) 
  3.     { 
  4.         this.x = x; 
  5.         this.y = y; 
  6.     } 
  7.  
  8.     add() 
  9.     { 
  10.         return this.x + this.y; 
  11.     } 
  12.  
  13. var p = new Point(1, 2); 
  14.  
  15. console.log(p.add()); // 输出3  

10. 模块化

JavaScript一直没有官方的模块化解决方案,开发者在实践中主要采用CommonJS和AMD规范。而ES6制定了模块(Module)功能。

不使用ES6

Node.js采用CommenJS规范实现了模块化,而前端也可以采用,只是在部署时需要使用Browserify等工具打包。这里不妨介绍一下CommenJS规范。

module.js中使用module.exports导出port变量和getAccounts函数:


 
 
  1. module.exports = { 
  2.   port: 3000, 
  3.   getAccounts: function() { 
  4.     ... 
  5.   } 
  6. }  

main.js中使用require导入module.js:


 
 
  1. var service = require('module.js'
  2. console.log(service.port) // 输出3000  

使用ES6

ES6中使用export与import关键词实现模块化。

module.js中使用export导出port变量和getAccounts函数:


 
 
  1. export var port = 3000 
  2. export function getAccounts(url) { 
  3.   ... 
  4. }  

main.js中使用import导入module.js,可以指定需要导入的变量:


 
 
  1. import {port, getAccounts} from 'module' 
  2. console.log(port) // 输出3000  

也可以将全部变量导入:


 
 
  1. import * as service from 'module' 
  2. console.log(service.port) // 3000  



作者:Fundebug

来源:51CTO

相关文章
|
4月前
|
缓存 前端开发 JavaScript
ES6 全部特性详解
ES6 是 JavaScript 语言的一个重要升级,它引入了大量新的功能,极大地增强了 JavaScript 的表达力和可读性。通过了解和掌握这些特性,开发者可以编写出更加简洁、高效、优雅的代码,并轻松应对大型项目的复杂性。
110 7
|
4月前
|
前端开发 网络架构
ES6对函数做了哪些扩展?
本文首发于微信公众号“前端徐徐”,介绍了 ES6 中函数参数的默认值、rest 参数、严格模式、name 属性、箭头函数、尾调用优化等新特性,并详细解释了各个特性的使用方法和注意事项。同时,还介绍了 ES2017 和 ES2019 中关于函数的一些改进,如函数参数尾逗号、`Function.prototype.toString()` 方法的修改以及 `catch` 语句参数的省略。
29 1
ES6新特性(基础常用)
文章介绍了ES6的常用新特性,包括let和const命令、对象解构赋值、模板字符串、Map和Set对象。通过示例代码详细解释了它们的用法和与传统ES5语法的区别,以及如何利用这些特性编写更简洁和高效的代码。
ES6新特性(基础常用)
|
网络架构
ES6学习(六)—函数的扩展
ES6学习(六)—函数的扩展
|
JavaScript 前端开发
ES6新增特性学习
ES6新增特性学习
|
JavaScript 前端开发
5个高级ES6特性
5个高级ES6特性
|
前端开发
前端学习笔记202306学习笔记第四十二天-Es8-ES8和ES9新增特性2
前端学习笔记202306学习笔记第四十二天-Es8-ES8和ES9新增特性2
78 0
|
前端开发
前端学习笔记202306学习笔记第四十二天-Es8-ES8和ES9新增特性1
前端学习笔记202306学习笔记第四十二天-Es8-ES8和ES9新增特性1
87 0
|
JSON JavaScript 前端开发
ES6特性总结
ES6特性总结
165 6
ES6特性总结
|
存储 JSON 自然语言处理
ES基础
ES基础
515 0