10个最佳Es6特性

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

1.函数参数默认值

2.模板字符串

3.多行字符串

4.解构赋值

5.对象属性简写

6.箭头函数

7.Promise

Let与Const

模块化

1. 函数参数默认值

不使用ES6

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

functionfoo(height, color)

{

varheight = height ||50;

varcolor = color ||'red';

//...

}

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

foo(0,"","")

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

使用ES6

functionfoo(height =50, color ='red')

{

// ...

}

2. 模板字符串

不使用ES6

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

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

使用ES6

将变量放在大括号之中:

varname =`Your name is${first}${last}.`

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

3. 多行字符串

不使用ES6

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

varroadPoem ='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

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

varroadPoem =`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

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

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

varhouse = data.house;

varmouse = data.mouse;

使用ES6

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

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

对于数组也是一样的:

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

5. 对象属性简写

不使用ES6

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

varbar ='bar';

varfoo =function()

{

// ...

}

varbaz = {

bar: bar,

foo: foo

};

使用ES6

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

varbar ='bar';

varfoo =function()

{

// ...

}

varbaz = { bar, foo };

6. 箭头函数

不使用ES6

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

functionfoo()

{

console.log(this.id);

}

varid =1;

foo();// 输出1

foo.call({id:2});// 输出2

使用ES6

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

varfoo =()=>{

console.log(this.id);

}

varid =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是异步编程串行化,避免了回调地狱:

varwait1000 =newPromise(function(resolve, reject)

{

setTimeout(resolve,1000);

});

wait1000

.then(function()

{

console.log("Hello");// 1秒后输出"Hello"

returnwait1000;

})

.then(function()

{

console.log("Fundebug");// 2秒后输出"Fundebug"

});

8. Let与Const

使用Var

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

{

vara =10;

}

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

使用let与const

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

{

leta =10;

}

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

constlet一样,也是块级作用域。

9. 类

不使用ES6

使用构造函数创建对象:

functionPoint(x, y)

{

this.x = x;

this.y = y;

this.add =function()

{

returnthis.x +this.y;

};

}

varp =newPoint(1,2);

console.log(p.add());// 输出3

使用ES6

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

classPoint

{

constructor(x, y)

{

this.x = x;

this.y = y;

}

add()

{

returnthis.x +this.y;

}

}

varp =newPoint(1,2);

console.log(p.add());// 输出3

10. 模块化

JavaScript一直没有官方的模块化解决方案,开发者在实践中主要采用CommonJSAMD规范。而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

varservice =require('module.js')

console.log(service.port)// 输出3000

使用ES6

ES6中使用exportimport关键词实现模块化。

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

exportvarport =3000

exportfunctiongetAccounts(url){

...

}

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

import{port, getAccounts}from'module'

console.log(port)// 输出3000

也可以将全部变量导入:

import*asservicefrom'module'

console.log(service.port)// 3000

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 译者按: ** 人生苦短,我用ES6**。 原文: Top 10 ES6 Features Every Busy ...
    Fundebug阅读 2,972评论 0 10
  • 原文: Top 10 ES6 Features Every Busy JavaScript Developer M...
    简书超级会员阅读 1,360评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,095评论 19 139
  • Node.js是目前非常火热的技术,但是它的诞生经历却很奇特。 众所周知,在Netscape设计出JavaScri...
    w_zhuan阅读 8,997评论 2 41
  • ES6(ECMAScript2015)的出现,无疑给前端开发人员带来了新的惊喜,它包含了一些很棒的新特性,可以更加...
    gtt21阅读 1,688评论 0 0

友情链接更多精彩内容