ES6新特性(学习笔记)

参考资料:

https://www.jianshu.com/p/87008f4f8513

https://www.cnblogs.com/liquanjiang/p/8909992.html

https://www.jianshu.com/p/ac1787f6c50f

https://blog.csdn.net/weixin_40736319/article/details/89301034

1、const 与 let 变量

  • let 表示声明变量,它的用法类似于var,但是所声明的变量,只在其所在的代码块内有效
  • const表示声明常量,只在其所在的代码块内有效。如果const的是一个对象,对象所包含的值是可以被修改的
const student = { name: 'cc' }

student.name = 'yy';// 不报错
student  = { name: 'yy' };// 报错
  • 当使用常量 const 声明时,请使用大写变量,如:CAPITAL_CASING
  • const 在声明时必须被赋值

2、模板字符串

  • 基本的字符串格式化。将表达式嵌入字符串中进行拼接。用${}来界定;
  • ES6反引号(``)直接搞定;
$("body").html(`This demonstrates the output of HTML content to the page, including student's ${name}, ${seatNumber}, ${sex} and so on.`);

3、箭头函数

    箭头函数就是函数的一种简写形式,使用括号包裹参数,跟随一个 =>,紧接着是函数体;

    箭头函数最直观的三个特点    

  • 不需要 function 关键字来创建函数
  • 省略 return 关键字
  • 继承当前上下文的 this 关键字
// ES5
var add = function (a, b) {
    return a + b;
};
// 使用箭头函数
var add = (a, b) => a + b;

// ES5
[1,2,3].map((function(x){
    return x + 1;
}).bind(this));
    
// 使用箭头函数
[1,2,3].map(x => x + 1);

4、函数的参数默认值

// ES6之前,当未传入参数时,text = 'default';
function printText(text) {
    text = text || 'default';
    console.log(text);
}

// ES6;
function printText(text = 'default') {
    console.log(text);
}

printText('hello'); // hello
printText();// default

5、神奇三点(Spread / Rest 操作符 ...)

  • 函数传参时,可使用...
function foo(x,y,z) {
  console.log(x,y,z);
}
 
let arr = [1,2,3];
foo(...arr); // 1 2 3
  • 数组的解构,构造
let arrs1 = ['aa', 'bb'];
let arrs2 = ['cc', 'dd'];

// 合并数组
let arrs = [...arrs1, ...arrs2];
console.log(arrs); // ['aa', 'bb', 'cc', 'dd']

// 析构数组
let param1, param2;
[param1, ...param2] = arrs1;

console.log(param1); // aa
console.log(param2); // ['bb']

6、解构赋值

// 对象
const student = {
    name: 'Sam',
    age: 22,
    sex: '男'
}

// ES5;
const name = student.name;
const age = student.age;
const sex = student.sex;
console.log(name + ' --- ' + age + ' --- ' + sex);

// ES6
const { name, age, sex } = student;
console.log(name + ' --- ' + age + ' --- ' + sex);

7、字面量简写

ES6中对象的属性和变量名一致时可省略变量名

//ES5
var type = 'rock';
var heat = '50%';
var music = {
  type: type,
  heat: heat
};
console.log(music);  // Object {type: "rock", heat: "50%"}

//ES6
var type = 'rock';
var heat = '50%';
var music = {
  type,
  heat
};
console.log(music);  // Object {type: "rock", heat: "50%"}

8、map和set

map用于映射,返回自己所需要的对象格式(即原数组映射成一个新的数组)

map方法接受一个新参数,这个参数就是将原数组变成新数组的映射关系。

function myfun_1(arr){
 var array = [];
  arr.map( item => {
    array.push(item*item);
  });
  console.log(array);
}
function myfun_2(arr){
 var array = [];
  arr.map( function(item){
  array.push(item*item);
 });
  console.log(array);
}

var arr3 = [1,2,3,4,5];
myfun_1(arr3);    //[1,4,9,16,25]
var arr1 = [5,2,1,3,4];
myfun_1(arr1);  //[25,4,1,9,16]
var arr2 = [3,4,5,1,2,6];
myfun_2(arr2);  //[9,16,25,1,4,36]

在实际的应用中,我们可以通过map方法得到某一个对象数组中特定属性的值

var obj = [
  {name:'小明',age:16,sex:'男'},
  {name:'小红',age:17,sex:'女'},
  {name:'小白',age:18,sex:'女'},
]
function getter(obj){
  obj.map( item => {
    console.log(item.age);
  })
}

getter(obj);
//16
//17
//18

set用于数组的去重

let set = new Set([1,2,3,3,3,3]);
console.log(set.size);//3

9、for...of 和 for...in

for...of 用于遍历一个迭代器,如数组:

let letters = ['a', 'b', 'c'];
letters.size = 3;
for (let letter of letters) {
  console.log(letter);
}

for...in 用来遍历对象中的属性或下标:

 let stus = ["Sam", "22", "男"];
 for (let stu in stus) {
   console.log(stus[stu]);
  }
// 结果: Sam, 22, 男

10、ES6中的类Class

ES6 中支持 class 语法,不过,ES6的class不是新的对象继承模型,它只是原型链的语法糖表现形式。

函数中静态方法使用 static 关键词定义构造函数的的方法和属性:

class Student {
  constructor() {
    console.log("I'm a student.");
  }
 
  study() {
    console.log('study!');
  }
 
  static read() {
    console.log("Reading Now.");
  }
}
 
console.log(typeof Student); // function
let stu = new Student(); // "I'm a student."
stu.study(); // "study!"
stu.read(); // "Reading Now."

类中的继承和超集:

class Tree {
  constructor(size = '10', leaves = {spring: 'green', summer: 'green', fall: 'orange', winter: null}) {
    this.size = size;
    this.leaves = leaves;
    this.leafColor = null;
  }

  changeSeason(season) {
    this.leafColor = this.leaves[season];
    if (season === 'spring') {
      this.size += 1;
    }
  }
}

class Maple extends Tree {
  constructor(syrupQty = 15, size, leaves) {
    super(size, leaves); //super用作函数
    this.syrupQty = syrupQty;
  }

  changeSeason(season) {
    super.changeSeason(season);//super用作对象
    if (season === 'spring') {
      this.syrupQty += 1;
    }
  }

  gatherSyrup() {
    this.syrupQty -= 3;
  }
}

注:

  • extends 允许一个子类继承父类,需要注意的是,子类的constructor 函数中需要执行 super() 函数。
  • 类的声明不会提升(hoisting),如果你要使用某个 Class,那你必须在使用之前定义它,否则会抛出一个 ReferenceError 的错误
  • 在类中定义函数不需要使用 function 关键词
  • super()必须在this之前使用
posted @ 2019-12-10 15:45  Katherine蓝羽  阅读(54)  评论(0)    收藏  举报