ES6

變量聲明:


除了 var ,我們現(xiàn)在還可以使用兩個新的標(biāo)示符來定義一個變量 ——letconst。它們的區(qū)別:

  1. letconst不存在變量提升(需先聲明再使用);
  2. const聲明的為常量,不可重復(fù)賦值(重復(fù)賦值會失敗但不會報錯);
  3. 重復(fù)聲明用const定義的變量會報錯;
  4. 通過var聲明的變量在函數(shù)內(nèi)都是可用的,而通過let聲明的變量則只屬于就近的花括號括起來的語句塊(屬于塊級作用域);
使用 var 的栗子:
//函數(shù)作用域內(nèi)也聲明了snack變量,變量得到提升。因if沒有執(zhí)行,所以snack的值為undefined
var snack = 'Meow Mix';

function getFood(food) {
    if (food) {
        var snack = 'Friskies';
        return snack;
    }
    return snack;
}

getFood(false); // undefined
使用let的栗子
//let聲明不提升,if未執(zhí)行,等同于函數(shù)作用域內(nèi)沒有聲明snack變量,會向作用域鏈上級查找
let snack = 'Meow Mix';

function getFood(food) {
    if (food) {
        let snack = 'Friskies';
        return snack;
    }
    return snack;
}

getFood(false); // 'Meow Mix'

立即執(zhí)行函數(shù):

//平時我們是這樣處理的
(function( doc ){
    var tit = doc.title;
})( document );
console.log( tit );        //Reference Error
//es6自執(zhí)行函數(shù):
{
    let tit= document.title;
}

console.log( tit ); // Reference Error

解構(gòu):


var [a, b, c] = [1, 2, 3]; let [head, ...tail] = [1, 2, 3, 4]; head // 1 tail // [2, 3, 4]
ES6允許按照一定模式,從數(shù)組和對象中提取值,對變量進(jìn)行賦值;

Math新增方法:


Math.trunc()
Math.trunc方法用于去除一個數(shù)的小數(shù)部分,返回整數(shù)部分。

 Math.trunc(4.1) // 4
 Math.trunc(4.9) // 4
 Math.trunc(-4.1) // -4
 Math.trunc(-0.1234) // -0
 Math.trunc('123.456')// 123
 Math.trunc(NaN);  // NaN
 Math.trunc('foo');  // NaN
 Math.trunc();  // NaN

Math.sign()
Math.sign方法用來判斷一個數(shù)到底是正數(shù)、負(fù)數(shù)、還是零。

  Math.sign(-5) // -1
  Math.sign(5) // +1
  Math.sign(0) // +0
  Math.sign(-0) // -0
  Math.sign(NaN) // NaN
  Math.sign('foo'); // NaN
  Math.sign();  // NaN

Math.cbrt()
Math.cbrt方法用于計(jì)算一個數(shù)的立方根

  Math.cbrt(-1) // -1
  Math.cbrt(0)  // 0
  Math.cbrt(1)  // 1
  Math.cbrt(2)  // 1.2599210498948734
  Math.cbrt('8') // 2
  Math.cbrt('hello') // NaN

Math.hypot()
Math.hypot方法返回所有參數(shù)的平方和的平方根

  Math.hypot(3, 4);  // 5
  Math.hypot(3, 4, 5);  // 7.0710678118654755
  Math.hypot();  // 0
  Math.hypot(NaN);  // NaN
  Math.hypot(3, 4, 'foo'); // NaN
  Math.hypot(3, 4, '5');  // 7.0710678118654755
  Math.hypot(-3);  // 3

數(shù)組的擴(kuò)展:


Array.from()
方法用于將兩類對象轉(zhuǎn)為真正的數(shù)組:類似數(shù)組的對象(array-like object)和可遍歷(iterable)的對象(包括ES6新增的數(shù)據(jù)結(jié)構(gòu)Set和Map)。

Array.of()
方法用于將一組值,轉(zhuǎn)換為數(shù)組。

  Array.of(3, 11, 8) // [3,11,8]
  Array.of(3) // [3]
  Array.of(3).length // 1

數(shù)組實(shí)例的copyWithin()
數(shù)組實(shí)例的copyWithin方法,在當(dāng)前數(shù)組內(nèi)部,將指定位置的成員復(fù)制到其他位置(會覆蓋原有成員),然后返回當(dāng)前數(shù)組。也就是說,使用這個方法,會修改當(dāng)前數(shù)組。

  [1, 2, 3, 4, 5].copyWithin(0, 3)// [4, 5, 3, 4, 5]

數(shù)組實(shí)例的find()和findIndex()
數(shù)組實(shí)例的find方法,用于找出第一個符合條件的數(shù)組成員。它的參數(shù)是一個回調(diào)函數(shù),所有數(shù)組成員依次執(zhí)行該回調(diào)函數(shù),直到找出第一個返回值為true的成員,然后返回該成員。如果沒有符合條件的成員,則返回undefined。

  [1, 4, -5, 10].find((n) => n < 0)// -5

數(shù)組實(shí)例的fill()
fill方法使用給定值,填充一個數(shù)組。

  ['a', 'b', 'c'].fill(7, 1, 2)// ['a', 7, 'c']
  ['a', 'b', 'c'].fill(7)// [7, 7, 7]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容