es6学习笔记入门总结

1.let const block 作用域

let 代替var 来声明块级作用域,没有变量提升,只在块内有作用

const 可以声明一个常量,类似于指针,指向某一个引用,这个常量并非一成不变的,但是不能重新赋值

const ARR=[5,6]

ARR.push(4);//[5,6,7]

ARR=[10];// TypeError

注意点:

  • let关键词声明的变量没有变量提升,
  • let 和const 只在最靠近的一个块中{}有效果(比如在for循环中)
  • 当使用const声明时,请使用大写变量,如:ARR
  • const在声明的时候必须被赋值,let 可以不用

2.箭头函数

就是函数的一种简写,使用(参数)=>{ }

  • 简洁函数体的函数,省略了return

let getPrice=function(){

return 4.5

}

等同于

let getPrice= ()=>4.5

  • 正经函数体

let arr=[1,2,3];
let arr1=arr.map(item=>{
return item+1
})
console.log(arr1);//[2,3,4]

  • 最大的好处,是他的this总是指向对象本身

原来的函数:
function Person() {
this.age=0;
let that=this;
setInterval(function () {
//console.log(this)//window
//this.age++//这句是不能执行的
//console.log(this.age)//NaN
that.age++
console.log(that.age)//需要用个变量来保存对象的this指向
},1000)
}


用了箭头函数可以这么写
function Person() {
this.age=0;
setInterval(()=>{
this.age++
console.log(this)//指向当前对象
},1000)
}

       var person=new Person();


3.函数参数默认值 允许对参数设置默认值

let getPrice=(price,tax=0.7)=>{

price=price+price*tax

}

getPrice(500); //850

 

4.Spread/Rest操作符 ...(未理解)

当用于迭代器中时,他是一个Spread操作符
function foo(x,y,z) {
console.log(x,y,z)
}
let arr=[1,2,3]
foo(...arr);//1 2 3
当用于函数传参的时候,是一个Rest操作符
function fooo(...args) {
console.log(args)
}
fooo(1,2,3,4,5,6);//[1,2,3,4,5,6]

5.对象词法扩展

es6允许使用简写方法声明对象字面量,来初始化属性变量和函数定义的方法,并且允许在对象属性中进行计算操作

function getCar(make, model, value) {
return {
// 简写变量
make, // 等同于 make: make
model, // 等同于 model: model
value, // 等同于 value: value

// 属性可以使用表达式计算值
['make' + make]: true,

// 忽略 `function` 关键词简写对象函数
depreciate() {
this.value -= 2500;
}
};
}

let car = getCar('Barret', 'Lee', 40000);

// output: {
// make: 'Barret',
// model:'Lee',
// value: 40000,
// makeKia: true,
// depreciate: function()
// }

6.二进制和八进制字面量(未用到过)

ES6 支持二进制和八进制的字面量,通过在数字前面添加 0o 或者0O 即可将其转换为八进制值:

let oValue = 0o10;
console.log(oValue); // 8

let bValue = 0b10; // 二进制使用 `0b` 或者 `0B`
console.log(bValue); // 2

7.对象和数组解构

解构可以避免在对象赋值时产生中间变量
function foo() {
return [1,2,3]
}

let [a,b,c]=foo()
console.log(a,b,c)

function bar() {
return {
x: 4,
y: 5,
z: 6
};
}
let {x: x, y: y, z: z} = bar();
console.log(x, y, z); // 4 5 6

其实就是可以一对一的赋值,比如

let [a,b,c]=[1,2,3];
console.log(a,b,c)//1,2,3

8.对象超类 super (未看懂)

ES6 允许在对象中使用 super 方法:

var parent = {
foo() {
console.log("Hello from the Parent");
}
}

var child = {
foo() {
super.foo();
console.log("Hello from the Child");
}
}

Object.setPrototypeOf(child, parent);
child.foo(); // Hello from the Parent
// Hello from the Child

9.模板语法${} 和分隔符 ` `(经常用到一定要记住!)

${ ... } 用来渲染一个变量
` 作为分隔符
let user = 'Barret';
console.log(`Hi ${user}!`); // Hi Barret!


10.for of 与for in的区别

for of 遍历一个迭代器和forEach 功能相同,都是取值,for in 是取属性比如{a:1},取出来的是a ,数组的话就是下标

(相当于 of 和each 的变量是value,而in 是key)

let xx=[1,3,4]
let yy={x:1,y:3,d:6};
xx.push(5)
xx.size=5;//这里是给数组多加了一个属性,而不是赋值
console.log(xx)
for(let i of xx){//遍历一个迭代器
console.log(i)//1,2,3,5 即数组的值
}
for(let i in xx){//用来遍历对象中的属性
console.log(i)//0,1,2,3,size
}
for(let i in yy){//用来遍历对象中的属性
console.log(i)//x,y,d
}

11.ES6 中两种新的数据结构集:Map 和 WeakMap(暂时未用到过)


map 和weakmap 每个对象都可以看成是一个map, 一个对象由多个key-val对构成,任何类型都可以作为对象的key

var myMap = new Map();

var keyString = "a string",
keyObj = {},
keyFunc = function () {};

// 设置值
myMap.set(keyString, "value 与 'a string' 关联");
myMap.set(keyObj, "value 与 keyObj 关联");
myMap.set(keyFunc, "value 与 keyFunc 关联");

myMap.size; // 3

// 获取值
myMap.get(keyString); // "value 与 'a string' 关联"
myMap.get(keyObj); // "value 与 keyObj 关联"
myMap.get(keyFunc); // "value 与 keyFunc 关联"


weakmap 就是一个map ,她的key 只能是对象,不用担心内存泄漏问题 只有四个方法 delete has(是否存在的意思,返回布尔值) get set

let w = new WeakMap();
w.set('a', 'b');
// Uncaught TypeError: Invalid value used as weak map key

var o1 = {},
o2 = function(){},
o3 = window;

w.set(o1, 37);
w.set(o2, "azerty");
w.set(o3, undefined);

w.get(o3); // undefined, because that is the set value

w.has(o1); // true
w.delete(o1);
w.has(o1); // false

12.set和WeakSet(暂时未用到过)

set对象是一组不重复的值,重复的将被忽略,值类型可以是原始类型和引用类型

  • 数组去重的好方法

let arr=[1,1,3,4,2,1]
let mySet=new Set(arr);
console.log(arr,mySet)

  • 可以通过forEach 和for of 来遍历

let arr=[1,1,3,4,2,1]
let mySet=new Set(arr);
mySet.add({a:1,b:2})
console.log(arr,mySet)
//可以通过forEach 和for of 来遍历
mySet.forEach(item=>console.log(item))//每项的值
for(let i of mySet){
console.log('属性'+i);
}

set 具有delete 和clear() 方法

WeakSet(未看懂干嘛用的)


类似于 WeakMap,WeakSet 对象可以让你在一个集合中保存对象的弱引用,在 WeakSet 中的对象只允许出现一次:


var ws = new WeakSet();
var obj = {};
var foo = {};

ws.add(window);
ws.add(obj);

ws.has(window); // true
ws.has(foo); // false, foo 没有添加成功

ws.delete(window); // 从结合中删除 window 对象
ws.has(window); // false, window 对象已经被删除

13.类 class 函数中使用static关键词定义构造函数的方法和属性

class Task{
constructor(){
console.log('i am coming')
}
showId(){
console.log(23)
}
static loadAll(){//这个是构造函数的方法
console.log('loading all tasks')
}
}
console.log(typeof Task)
let task1=new Task();//这里创建了一个task对象
console.log(task1)//Task {} 有个构造函数上的方法loadAll 和普通的方法showId
task1.showId();
//task1.loadAll();不能访问构造函数上的方法
Task.loadAll();

类中的继承和超集

class Car{
constructor(){
console.log('creating a new car')
}
name(){
console.log('didi')
}
static name1(){
console.log('aa')
}
}
class Porsche extends Car{
constructor(){
super();
console.log('creating porsche')
}
}
let c=new Porsche();
let c1=new Car();
c.name()

注意点:

  • 类的声明不会提升,必须再使用之前定义他 ,否则会referenceError 引用错误
  • 在类中定义函数不需要使用function 关键词
  • Symbol 是一种新的数据类型,他的值是唯一的,不可变的,目的是为了生成一个唯一的标识符,

14.迭代器 iterators (暂时未用到过)

var arr=[1,2,3]
var itr=arr[Symbol.iterator]()
itr.next();
itr.next();
itr.next();
itr.next();


15.Generators 是es6的新特性,他允许一个函数返回的可遍历对象生成多个值,在使用中你会看到*语法和关键词yield: (暂时未用到过据说很重要)

  1. function *infiniteNumbers() {
  2. var n = 1;
  3. while (true){
  4. yield n++;
  5. }
  6. }
  7. var numbers = infiniteNumbers(); // returns an iterable object
  8. console.log(numbers.next()) ;// { value: 1, done: false }
  9. numbers.next(); // { value: 2, done: false }
  10. numbers.next(); // { value: 3, done: false }


16.promises 是一个等待被异步执行的对象,当他执行完成后,其状态会变成resolved 或者rejected.(也是特别的重要)

var p = new Promise(function(resolve, reject) {
if (/* condition */) {
// fulfilled successfully
resolve(/* value */);
} else {
// error, rejected
reject(/* reason */);
}
});


每一个 Promise 都有一个 .then 方法,这个方法接受两个参数,第一个是处理 resolved 状态的回调,一个是处理 rejected 状态的回调:

p.then((val) => console.log("Promise Resolved", val),
(err) => console.log("Promise Rejected", err));

原文地址:https://www.cnblogs.com/cytheria/p/8686851.html