ES6、ES7的一些新特性

1.常见的就是let 和 const 命令

let 只在命令所在的代码块内有效

const声明一个只读的常量

2.变量的赋值

let [a, b, c] = [1, 2, 3];

这样输出的话a=1,b=2

let { foo, bar } = { foo: 'aaa', bar: 'bbb' };

对象也可以 foo='aaa'

var {x, y = 5} = {x: 1};

上面的是默认值x=1,y=5。当y为undefined是取默认值

3.字符串之模板字符串

let name = "jasid";
let age = "12";
let str = `姓名${name},年龄${age}`;
输出 姓名jasid,年龄12
哈哈,跟c#6差不多

4.函数扩展

function log(x, y = 'World') {
  console.log(x, y);
}

函数默认值log('Hello') 输出 Hello World

log('Hello', 'China') 输出 Hello China

log('Hello', '') 输出 Hello

 

箭头函数

var f = v => v;

// 等同于
var f = function (v) {
  return v;
};
function(res) {

})
//可以写成
res => {
}

 ES7的新特性

includes()函数用来判断一个数组是否包含一个指定的值,如果包含则返回true,否则返回false。

includes函数与indexOf 函数很相似

//在ES7之前的做法

//使用indexOf()验证数组中是否存在某个元素,这时需要根据返回值是否为-1来判断:

let arr = ['react', 'angular', 'vue'];

if (arr.indexOf('react') !== -1)

{

    console.log('react存在');

}

//使用ES7的includes()

//使用includes()验证数组中是否存在某个元素,这样更加直观简单:

let arr = ['react', 'angular', 'vue'];

if (arr.includes('react'))

{

    console.log('react存在');

}

猜你喜欢

转载自www.cnblogs.com/heyiping/p/11889626.html