es6小记

来源:互联网 发布:新闻英语听力训练软件 编辑:程序博客网 时间:2024/05/29 17:01

const、let关键字

let:声明 使js存在块级作用域
const :定义常亮,定义以后不可以更改。若是引用的类型,那可以更改他的属性。
const a={b:789};
a.b='45456';
//可以正常运行。


函数

  1. 箭头函数 箭头函数永远是匿名的。
let add = (a, b) => {        return a + b    }    let add1=function (a,b) {        return a + b    }    console.log(add(1, 3));    console.log(add1(1, 3));

2.this的使用

原来:

let age=2;    let kit={        age:1,        grow:function () {            setTimeout(function () {                console.log(++this.age)            }.bind(this),100)        }    };    kit.grow();//2

或者

let age=2;    let kit={        age:1,        grow:function () {            const that=this;            setTimeout(function () {                console.log(++that.age)            },100)        }    };    kit.grow();//2

现在:
let age=2;
let kit={
age:1,
grow:function () {
setTimeout( ()=> {
console.log(++this.age)
},100)
}
};
kit.grow();
//2

3.展开运算符...

let arr1=[1,2,3];
let arr2=[4,2,3];
let arr3=[...arr1,...arr2]
console.log(arr3)
//[1,2,3,4,2,3]
在函数中
function test(...args) {
console.log(args);
}
test(1,2,3)//[1, 2, 3]

4.模板字符串``

let name='tom';
let a=
my name is ${name} !;
console.log(a);
let song=
long long ago,
there is a hall in a country
console.log(song);
// my name is tom !
// long long ago,
// there is a hall in a country

5.模块

export导出这个模块
function hello() {
console.log('hello')
}
export hello;

import加载这个模块
import {PI,hello,person} from './hello';