国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > JS > 正文

ES6 新增的創(chuàng)建數(shù)組的方法(小結(jié))

2024-05-06 16:53:54
字體:
供稿:網(wǎng)友

在ES6之前,創(chuàng)建數(shù)組的方式有2種:

一: 通過數(shù)組字面量

let array = [1,2,3];console.log(array);//[1,2,3]

二: 通過new Array()創(chuàng)建數(shù)組

let array = new Array(1, 2, 3);console.log(array); //[1,2,3]

在大多數(shù)情況下new Array()運行良好:

let array = new Array(1, 2);console.log(array.length); //2console.log(array[0]); //1console.log(array[1]); //2array = new Array('a');console.log(array.length); //1console.log(array[0]);//'a'array = new Array(1, 'a');console.log(array.length); // 2console.log(array[0]);//1console.log(array[1]);//'a'

但是new Array()有一種詭異的情況:

let array = new Array(2);console.log(array[0]); // undefinedconsole.log(array[1]);// undefinedconsole.log(array.length); // 2 

當(dāng)我們給new Array()傳遞單個數(shù)字參數(shù)時,這個數(shù)字不是作為數(shù)組元素,而是該數(shù)組的length屬性而存在,而數(shù)組本身則是一個空數(shù)組。

為了解決上面這個令人類沒有安全感的特性,ES6引入了Array.of()來解決這個問題:

三:Array.of()

顧名思義,of()方法就是以它接受到的參數(shù)作為元素來創(chuàng)造數(shù)組,上面我們說的單個數(shù)字參數(shù)的情況也同樣適用:

let array = Array.of(3);console.log(array.length); // 1console.log(array[0]); // 3array = Array.of(1, 2);console.log(array.length);// 2console.log(array[0]); // 1console.log(array[1]);// 2array = Array.of('a');console.log(array.length);// 1console.log(array[0]);// 'a'array = Array.of(1, 'a');console.log(array.length); // 2console.log(array[0]);// 1console.log(array[1]);// 'a'

四:Array.from() 

ES6還增加了一個Array.from(),也是用了創(chuàng)建一個數(shù)組。它主要用在以類數(shù)組對象和可迭代對象為藍(lán)本,創(chuàng)建對應(yīng)的數(shù)組。

1: Array.from(類數(shù)組對象)

我們最熟悉的類數(shù)組對象,應(yīng)該就是function的arguments對象了。接下來,我們看一個用Array.from()創(chuàng)建包含arguments元素的數(shù)組:

function createArrayFrom() {  console.log(arguments instanceof Array); // false  return Array.from(arguments);}let array = createArrayFrom(1, 2, 3);console.log(array instanceof Array); // trueconsole.log(array.length); //3console.log(array[0]);//1console.log(array[1]);//2console.log(array[2]);//3console.log(array.indexOf(2)); //1

2: Array.from(可迭代對象)

Array.from()也可以把一個可迭代對象轉(zhuǎn)換為數(shù)組:

let iteratorObject = {  *[Symbol.iterator](){    yield 1;    yield 2;    yield 3;  }};let array = Array.from(iteratorObject);console.log(array instanceof Array); // trueconsole.log(array.length); // 3console.log(array[0]); // 1

五:Array.from()的第二個參數(shù) 

前面的例子,我們看到了一個類數(shù)組對象和可迭代對象作為Array.from()的第一個參數(shù),從而創(chuàng)建出包含它們元素的數(shù)組。Array.from()的第二個參數(shù)是一個函數(shù),這個函數(shù)用來將類數(shù)組對象和可迭代對象的元素進(jìn)行某種變換后,再作為生出數(shù)組的元素,例如:

let iteratorObject = {  *[Symbol.iterator](){    yield 1;    yield 2;    yield 3;  }};let array = Array.from(iteratorObject, (item)=>{return item + 1});console.log(array[0]); //2console.log(array[1]); //3console.log(array[2]); //4

這個例子里,我們提供了把每個元素值加一的變換,所以原本的1,2,3,置換到新的數(shù)組之后,元素是2,3,4。

六: Array.from()的第三個參數(shù)

Array.from()還提供第三個參數(shù)可用,第三個參數(shù)用來指定this的值,如果在整個方法的調(diào)用中有用到this 的話,看一個例子:

let firstHelper = {  diff: 1,  add(value){    return value + this.diff;  }};let secondHelper = {  diff: 2};function createArrayFrom() {  return Array.from(arguments, firstHelper.add, secondHelper);}let array = createArrayFrom(1, 2, 3);console.log(array); //[3, 4, 5]

上面的例子里面,我們的在add()方法里面使用了this(這行代碼:value + this.diff),并且add()定義在firstHelper對象,且firstHelper對象也有diff屬性,但是我們的第三個參數(shù)傳入的是secondHelper,所以在firstHelper.add()方法里的this值是secondHelper。

以上就是ES6新增的Array.of()和Array.from()方法,可以使得開發(fā)者用更少的代碼應(yīng)對更多變的創(chuàng)建數(shù)組的場景。


注:相關(guān)教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 永康市| 筠连县| 讷河市| 库伦旗| 肥东县| 塔河县| 绥阳县| 永清县| 兴文县| 清镇市| 左权县| 迁安市| 扬州市| 珠海市| 泽库县| 客服| 莱州市| 红安县| 石渠县| 丹江口市| 昌图县| 阿拉尔市| 石阡县| 河间市| 遂平县| 邯郸县| 邹城市| 青冈县| 阿巴嘎旗| 华宁县| 青铜峡市| 贡觉县| 甘谷县| 岚皋县| 离岛区| 大冶市| 灌南县| 互助| 安化县| 西乡县| 宝兴县|