JavaScript Good Parts學習筆記-數組篇

來源:互聯網
上載者:User

標籤:for   make   出錯   注意   整數   ret   var   cti   numbers   

數組是一段線性分配的記憶體。通過計算位移來訪問其中的元素。

不幸的是,JavaScript並沒有像數組一樣的資料結構

作為替代,JavaScript提供了類數組的對象,把數組下標改為字串。

1 數組字面量(Array Literals)---繼承自Array.prototype,所以繼承了大量有用的方法,比如length屬性

var empty = [];
var numbers = [
    ‘zero‘, ‘one‘, ‘two‘, ‘three‘, ‘four‘,
    ‘five‘, ‘six‘, ‘seven‘, ‘eight‘, ‘nine‘
];
empty[1] // undefined
numbers[1] // ‘one‘
empty.length // 0
numbers.length // 10

對象字面量---繼承自Object.prototype

var numbers_object = {
    ‘0‘: ‘zero‘,
    ‘1‘: ‘one‘,
    ‘2‘: ‘two‘,
    ‘3‘: ‘three‘,
    ‘4‘: ‘four‘,
    ‘5‘: ‘five‘,
    ‘6‘: ‘six‘,
    ‘7‘: ‘seven‘,
    ‘8‘: ‘eight‘,
    ‘9‘: ‘nine‘
};

註:在大多數語言中,數組對象要求裡面元素類型一致,但是JavaScript不是這樣的。

2 長度(length)

和大多數語言不同,JavaScript的數組length是沒有上界的,如果用大於當前length的下標儲存,不會出錯,

數組會被自動擴容。

 

3 刪除(Delete)

因為JavaScript的數組就是對象,所以可以用delete運算子從數組中刪除對象

delete numbers[2];
// numbers is [‘zero‘, ‘one‘, undefined, ‘shi‘, ‘go‘]

這樣刪除後,會在原來的位置留下個“空洞”

所以正確的刪除應該是用splice方法 第一個參數是數組中序號,第二個參數是刪除的個數

numbers.splice(2, 1);
// numbers is [‘zero‘, ‘one‘, ‘shi‘, ‘go‘]

註:對於大型數組來說,效率會不高。

4 枚舉(Enumeration)

注意,for in 無法保證順序,而且可能取得原型鏈上的屬性,所以不推薦使用。

常規的for沒有問題。代碼如下

var i;
for (i = 0; i < myArray.length; i += 1) {
document.writeln(myArray[i]);
}

 

5 容易混淆的地方(Confusion)

常見的錯誤是數組和對象混用。

規則其實很簡單,當屬性名稱是小而連續的整數時,使用數組,否則就使用對象。

可以用下面的方法判斷是否數組

var is_array = function (value) {
return Object.prototype.toString.apply(value) === ‘[object Array]‘;
};

 

6 方法(Methods)

JavaScript提供了一套數組可用的方法,是儲存在Array.prototype中的函數

我們可以擴充他,比如增加一個對數組進行計算的方法

Array.prototype.reduce = function(f, value) {
    var i;
    for (i = 0; i < this.length; i += 1) {
        value = f(this[i], value);
    }
    return value;
};

使用這個方法的例子。

// Create an array of numbers.
var data = [4, 8, 15, 16, 23, 42];
// Define two simple functions. One will add two
// numbers. The other will multiply two numbers.
var add = function(a, b) {
    return a + b;
};
var mult = function(a, b) {
    return a * b;
};
// Invoke the data‘s reduce method, passing in the
// add function.
var sum = data.reduce(add, 0); // sum is 108
// Invoke the reduce method again, this time passing
// in the multiply function.
var product = data.reduce(mult, 1);
// product is 7418880

7 指定初始值(Dimensions)

Array.dim = function (dimension, initial) {
var a = [], i;
for (i = 0; i < dimension; i += 1) {
a[i] = initial;
}
return a;
};
// Make an array containing 10 zeros.
var myArray = Array.dim(10, 0);

JavaScript Good Parts學習筆記-數組篇

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.