zl程序教程

您现在的位置是:首页 >  后端

当前栏目

关于ES6的10个最佳特性

ES6 关于 10 最佳 特性
2023-09-27 14:23:55 时间

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


foo(0, "", "") 

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

使用ES6



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 };  

6. 箭头函数

不使用ES6

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



使用ES6

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


var foo = () =  {    console.log(this.id);  var id = 1;  foo(); // 输出1  foo.call({ id: 2 }); // 输出1  

7. Promise

不使用ES6

嵌套两个setTimeout回调函数:


setTimeout(function()      console.log(Hello); // 1秒后输出"Hello"      setTimeout(function()      {          console.log(Fundebug); // 2秒后输出"Fundebug"      }, 1000);  }, 1000);  

使用ES6

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


var wait1000 = new Promise(function(resolve, reject)      setTimeout(resolve, 1000);  wait1000      .then(function()      {          console.log("Hello"); // 1秒后输出"Hello"          return wait1000;      })      .then(function()      {          console.log("Fundebug"); // 2秒后输出"Fundebug"      });  

8. Let与Const

使用Var

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


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

使用let与const

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


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

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

9. 类

不使用ES6

使用构造函数创建对象:



10. 模块化

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

不使用ES6

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

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


module.exports = {    port: 3000,    getAccounts: function() {      ... 

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


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

使用ES6

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

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


export var port = 3000  export function getAccounts(url) {    ... 

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


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

也可以将全部变量导入:



分分钟带你了解 ES2022 最重要的 4 个特性! ECMAScript 2022 将于今年 6 月发布,本篇带来 ES2022 肯定会出现的最重要的 4 个变化!因为这些特性已经进入了 TC39 标准化发布的 第 4 个阶段 了。
ES6 对象的扩展 对象(object)是 JavaScript 最重要的数据结构。ES6 对它进行了重大升级 属性的简洁表示法 ES6 允许在大括号里面,直接写入变量和函数,作为对象的属性和方法。这样的书写更加简洁。 const foo = bar const baz = {foo}; baz // {foo: bar } // 等同于 const baz = {foo: foo};
聊聊ES7与ES8特性 我曾写过一篇关于ES6博客《10个最佳ES6特性》,这次我打算聊聊ES7和 ES8特性。 ES7只有2个特性: • includes() • 指数操作符 ES8尚未发布(2017年1月),下面是它已经完成起草的一些特性: • Object.values() • Object.entries() • padStart() • padEnd() • Object.getOwnPropertyDescriptors() • 函数参数列表结尾允许逗号 • Async/Await