1偎漫、變量
在ES6之前,變量聲明關(guān)鍵字:var
var的缺點:
(1)var可以多次聲明同一個變量--在其他編程語言是沒辦法想象的
var a =0;
var a = 99;
(2)var會造成變量提升
(function rr() {
if(true) {
var a = 666;
}
console.log(a); //輸出666
})()
ES6變量聲明關(guān)鍵字:let 變量聲明方篮;const 常量聲明名秀。解決了var前面提到的兩個缺點。
let以及const都是塊級作用域藕溅。
如何理解塊級作用域
在一個函數(shù)內(nèi)部 function (){}
在一個代碼塊內(nèi)部
常見面試題:
for(var a = 0; a < 3; a++) {
setTimeout(function() {
console.log(a);
}, 1000)
}
//輸出結(jié)果都為3
如何將結(jié)果輸出為0, 1, 2
es5處理方法 -- 用函數(shù)制造塊級作用域
for(var a = 0; a < 3; a++) {
(function (a) {
setTimeout(function() {
console.log(a);
}, 1000)
})(a)
}
//輸出結(jié)果0,1,2
es6處理方法 -- 更加簡單明了
for(let a = 0; a < 3; a++) {
setTimeout(function() {
console.log(a);
}, 1000)
}
//輸出結(jié)果為:0,1,2
2匕得、函數(shù) (箭頭函數(shù))
(1)不需要function關(guān)鍵字來創(chuàng)建函數(shù)
es5創(chuàng)建函數(shù)
var aa = function() {}
es6創(chuàng)建函數(shù)
var aa = () => {}
(2)可以極大的簡寫函數(shù)
es5函數(shù)
var fn = function(a) {
return a + 5
}
可以利用箭頭函數(shù)簡寫為:
var fn = a => a + 5;
簡寫規(guī)則:
當函數(shù)所傳參數(shù)只有一個時,可以去掉(); eg: (a) => {} 簡寫為:a => {}
當函數(shù)體中只返回值蜈垮,而沒有其他操作時耗跛,可以去掉{}裕照;eg: (a, b) => {return a+b} 簡寫為:(a, b) => a+b(3)繼承上下文的關(guān)鍵字this
es5繼承上下文的關(guān)鍵字
var fn = function() {
}
fn.bind(this)
es6繼承上下文的關(guān)鍵字
var fn = () => {}
3攒发、數(shù)組
新增常用的4個方法
(1)map --映射
let arr=[66,59,80];
let result=arr.map(item => {
if(item >= 60){
return "及格"
}else{
return "不及格"
}
});
//result:["及格", "不及格", "及格"]
(2)reduce --匯總
let arr = [12,69,180,8763];
let result = arr.reduce((tmp, item, index) => {
console.log(tmp, item, index);
return tmp + item;
});
console.log(result);//求和
(3)filter --過濾
let arr=[
{title: '電源線', price: 50},
{title: '電腦', price: 13000},
{title: '鍵盤', price: 120},
{title: '手機', price: 9000}
];
let result=arr.filter(json=>json.price>=5000);
console.log(result);//[{title: '電腦', price: 13000},{title: '手機', price: 9000}]
(4)forEach --迭代
let arr=[12,5,8,9];
arr.forEach((item,index)=>{
console.log(index+': '+item); //0: 12 1: 5 2: 8 3: 9
});
4、函數(shù)的參數(shù)
(1)收集參數(shù)
function show(a, b, ...args){
console.log(args)
}
show(1,2,3,4,5,6) //輸出[3,4,5,6]
(2)展開數(shù)組
const arr1 = [1,2,3];
const arr2 = [4,5,6];
console.log([...arr1, ...arr2]); //輸出[1,2,3,4,5,6]
console.log(...arr1); //輸出1晋南,2惠猿,3
(3)默認值
const show = (a=666, b=999) => {
console.log(a); //輸出666
console.log(b); //輸出999
}
show();
5、解構(gòu)賦值
(1)左右兩邊結(jié)構(gòu)必須一樣负间;
(2)右邊必須是個合法的值偶妖;
(3)聲明和賦值不能分開(必須在一句話里完成);
let [a,b,c]=[1,2,3,999];
let {e,d,g}={e: 4, d: 5, f: 6, g: 7};
console.log(a,b,c,e,d,g);
//輸出1 2 3 4 5 7
6政溃、字符串
(1)多了兩個新方法: startsWith endsWith
//startsWith
let str='https//http://mp.blog.csdn.net/postedit/79478118';
if(str.startsWith('http://')){
alert('普通網(wǎng)址');
}else if(str.startsWith('https://')){
alert('加密網(wǎng)址');
}else if(str.startsWith('git://')){
alert('git地址');
}else if(str.startsWith('svn://')){
alert('svn地址');
}else{
alert('其他');
}
//endsWith
let str='12121.png';
if(str.endsWith('.txt')){
alert('文本文件');
}else if(str.endsWith('.jpg')){
alert('JPG圖片');
}else{
alert('其他');
}
(2)字符串模板
優(yōu)點:方便字符串拼接趾访;可以折行
let title='標題';
let content='內(nèi)容';
let str='<div>\
<h1>'+title+'</h1>\
<p>'+content+'</p>\
</div>';
let str2=`<div>
<h1>${title}</h1>
<p>${content}</p>
</div>`;