(1)let
區(qū)別1:
let定義的變量只能在代碼塊{}里面使用,形成一個{}塊級作用域
var作用于函數(shù)作用域
區(qū)別2:
let不允許重復聲明,它是一個封閉空間,解決了i問題
var a=5
var a=12 //-->a=5
let a=5
let a=12 //-->error
區(qū)別3:
const
const用來定義常量,一旦定義了,后面就不能修改了
(2)箭頭函數(shù)=>
*function()中this是指向調(diào)用這個函數(shù)的對象
箭頭函數(shù)中,this指向了window
arguments,不能使用
由于this在箭頭函數(shù)中已經(jīng)按照詞法作用域綁定了,所以实蔽,用call()或者apply()調(diào)用箭頭函數(shù)時男应,無法對this進行綁定鹅经,即傳入的第一個參數(shù)被忽略
(3)解構(gòu)賦值
左右兩邊結(jié)構(gòu)一樣
右邊必須是合法結(jié)構(gòu)(錯誤例子:{12,5,8})
聲明和賦值寫在一句話中(錯誤例子:let [a,b]; [a,b]=[12,5];)
//數(shù)組
let [a,b,c]=[12,5,101] // a=12,b=5,c=101
//JSON
let {a,b,c}={b:5,a:12,c:101} // json跟順序無關(guān),a=12,b=5,c=101
//混合型,左右結(jié)構(gòu)陕习、順序一致
let [{a,b},[n1,n2,n3],num,str] = [{a:12,b:5},[12,5,8],8,'EcmaScript']
//a=3,b=8,c is not defined? ? 對數(shù)組的解構(gòu)
var [a,b]=[3,5,8];
//x=V,y=u,z=e? ? 對字符串的結(jié)構(gòu)
var [x,y,z]=Vue;
//m=10,n=20? ? 對象的解構(gòu),對key進行拆分
var {n,m}={m:10,n:20};
(4)復制數(shù)組
var arr1=[1,2,3,4]
for(var i=0;i<arr1.length;i++){
? ? arr2[i]=arr1[i]
}
arr2=Array.from(arr1)
(5)JSON
//簡寫誉帅,當key,value一樣時,可以只寫一個
{m:m,n:n}--->{m,n}
//其中也可以有key闸餐、value不相同的
let a=12;
let b=5;
let json={a,b,c:8};? //{a:a,b:b,c:8}
console.log(json);? //{a:12,b:5,c:8}
//JSON里的方法簡寫,`:function`可以省略
let json={
? a:12,
? show(){? //show:function(){
? ? alert(this.a)
? }
}
(6)字符串模板?``(反單引號)
用于字符串連接矾芙,可以換行
`string text ${expression} string text`
(7)rest參數(shù)...(3個點)
var [x,...y]=[4,8,10,30] //x=4,y=[8,10,30] y成了動態(tài)參數(shù)
? let xy=[...'es6'];? //['e','s','6']? 拆解
? //數(shù)組拆分
? console.log(...[4,8])? //4,8
? //數(shù)組拆分的進階
? let arr1 =[1,3];let arr2=[4,8];
? [...arr1,...arr2]? //[1,3,4,8]
(8)默認參數(shù)
function show(a,b=5,c=12){
? console.log(a,b,c);
}
show(99,19)? ? //99,19,12
show(99)? ? //99,5,12
(9)for of循環(huán)
//for in
var arr=['apple','banana','orange','pear']
for(var i in arr){
? ? console.log(i); // for in 出來的是 索引0 1 2 3
}
//for of
var arr=['apple','banana','orange','pear']
for(var i of arr){
? ? console.log.(i); // for of 出來的是 實際apple,banana等
}
for of可以循環(huán)數(shù)組,但是不能循環(huán)json,因為它是為了與map配合使用
(10)Map(數(shù)據(jù)結(jié)構(gòu))
與for of配合使用近上,是一個key剔宪、value
var map = new Map();
map.set(name,value); // 可以設置name,value
map.get(name); // 可以單獨獲取name
map.delete(name); // 可以單獨刪除name
//與for of 的配合
for(var name of map){
? ? console.log(name);? // a,apple? b,banana
}
for(var [key,value] of map){
? ? console.log(key,value); // key,value
}
for(var key of map.keys) // 可以只循環(huán)map中的key值
(11)map(數(shù)組)
映射–原數(shù)組映射成新數(shù)組,一個對應一個
//例一
? let arr=[12,5,8];
? ? let result=arr.map(item=>item*2);
? ? alert(result);
//例二
? ? let score=[19, 85, 99, 25, 90];
? ? let result=score.map(item=>item>=60?'及格':'不及格');
? ? alert(score);
? ? alert(result);
(12)reduce(數(shù)組)
匯總–求和–多個變成一個
//求和
let arr=[12,69,180,8763];
? ? let result=arr.reduce(function (tmp, item, index){
? ? ? //alert(tmp+','+item+','+index);
? ? ? return tmp+item;
? ? });
? ? alert(result);
//求平均數(shù)
let arr=[12,69,180,8763];
? ? let result=arr.reduce(function (tmp, item, index){
? ? ? if(index!=arr.length-1){ //不是最后一次
? ? ? ? return tmp+item;
? ? ? }else{? ? ? ? ? ? ? ? ? ? //最后一次
? ? ? ? return (tmp+item)/arr.length;
? ? ? }
? ? });
? ? alert(result);
(13)filter(數(shù)組)
過濾器
let arr=[12,5,8,99,27,36,75,11];
? ? //選擇能夠被3整除的數(shù)
? ? let result=arr.filter(item=>item%3==0);
? ? alert(result);
(14)forEach(數(shù)組)
循環(huán)–迭代
let arr=[12,5,8,9];
? ? arr.forEach((item,index)=>{
? ? ? alert(index+': '+item);
? ? });
(15)面向?qū)ο骳lass
使用了class關(guān)鍵字壹无,現(xiàn)在構(gòu)造函數(shù)和類分開了
class里面直接加方法
//old
? ? function User(name, pass){
? ? ? this.name=name;
? ? ? this.pass=pass;
? ? }
? ? User.prototype.showName=function (){
? ? ? alert(this.name);
? ? };
? ? User.prototype.showPass=function (){
? ? ? alert(this.pass);
? ? };
//ES6
? ? class User{
? ? ? constructor(name, pass){
? ? ? ? this.name=name;
? ? ? ? this.pass=pass;
? ? ? }
? ? ? showName(){
? ? ? ? alert(this.name);
? ? ? }
? ? ? showPass(){
? ? ? ? alert(this.pass);
? ? ? }
? ? }
(16)繼承extends
old:?子類.prototype = new 父類
ES6:?class 子類 extends 父類{}
supper關(guān)鍵字:超類葱绒、父類
//old
function VipUser(name, pass, level){
? ? ? User.call(this, name, pass);
? ? ? this.level=level;
? ? }
? ? VipUser.prototype=new User();
? ? VipUser.prototype.constructor=VipUser;
? ? VipUser.prototype.showLevel=function (){
? ? ? alert(this.level);
? ? };
//ES6? ?
class VipUser extends User{
? ? ? constructor(name, pass, level){
? ? ? ? super(name, pass);
? ? ? ? this.level=level;
? ? ? }
? ? ? showLevel(){
? ? ? ? alert(this.level);
? ? ? }
? ? }
(17)模塊化
exprot default?// 導出
import modA from './a.js'?// 引入模塊