JavaScript深入系列第十三篇,講解類數組對象與對象的相似與差異以及arguments的注意要點
類數組對象
所謂的類數組對象:
擁有一個 length 屬性和若幹索引屬性的對象
舉個例子:
var array = ['name', 'age', 'sex'];var arrayLike = { 0: 'name', 1: 'age', 2: 'sex', length: 3}
即便如此,為什麼叫做類數組對象呢?
那讓我們從讀寫、擷取長度、周遊三個方面看看這兩個對象。
讀寫
console.log(array[0]); // nameconsole.log(arrayLike[0]); // namearray[0] = 'new name';arrayLike[0] = 'new name';
長度
console.log(array.length); // 3console.log(arrayLike.length); // 3
周遊
for(var i = 0, len = array.length; i < len; i++) { ……}for(var i = 0, len = arrayLike.length; i < len; i++) { ……}
是不是很像?
那類數組對象可以使用數組的方法嗎?比如:
arrayLike.push('4');
然而上述代碼會報錯: arrayLike.push is not a function
是以終歸還是類數組呐……
調用數組方法
如果類數組就是任性的想用數組的方法怎麼辦呢?
既然無法直接調用,我們可以用 Function.call 間接調用:
var arrayLike = {0: 'name', 1: 'age', 2: 'sex', length: 3 }Array.prototype.join.call(arrayLike, '&'); // name&age&sexArray.prototype.slice.call(arrayLike, 0); // ["name", "age", "sex"] // slice可以做到類數組轉數組Array.prototype.map.call(arrayLike, function(item){ return item.toUpperCase();}); // ["NAME", "AGE", "SEX"]
類數組轉對象
在上面的例子中已經提到了一種類數組轉數組的方法,再補充三個:
var arrayLike = {0: 'name', 1: 'age', 2: 'sex', length: 3 }// 1. sliceArray.prototype.slice.call(arrayLike); // ["name", "age", "sex"] // 2. spliceArray.prototype.splice.call(arrayLike, 0); // ["name", "age", "sex"] // 3. ES6 Array.fromArray.from(arrayLike); // ["name", "age", "sex"] // 4. applyArray.prototype.concat.apply([], arrayLike)
那麼為什麼會講到類數組對象呢?以及類數組有什麼應用嗎?
要說到類數組對象,Arguments 對象就是一個類數組對象。在用戶端 JavaScript 中,一些 DOM 方法(document.getElementsByTagName()等)也傳回類數組對象。
Arguments對象
接下來重點講講 Arguments 對象。
Arguments 對象隻定義在函數體中,包括了函數的參數和其他屬性。在函數體中,arguments 指代該函數的 Arguments 對象。
舉個例子:
function foo(name, age, sex) { console.log(arguments);}foo('name', 'age', 'sex')
列印結果如下:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5CMxEjN3IDOiVDN4MWY1UmY3QjY3MDM3MWNldjNiNGZj9CX0JXZ252bj91Ztl2Lc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
我們可以看到除了類數組的索引屬性和length屬性之外,還有一個callee屬性,接下來我們一個一個介紹。
length屬性
Arguments對象的length屬性,表示實參的長度,舉個例子:
function foo(b, c, d){ console.log("實參的長度為:" + arguments.length)}console.log("形參的長度為:" + foo.length)foo(1)// 形參的長度為:3// 實參的長度為:1
callee屬性
Arguments 對象的 callee 屬性,通過它可以調用函數自身。
講個閉包經典面試題使用 callee 的解決方法:
var data = [];for (var i = 0; i < 3; i++) { (data[i] = function () { console.log(arguments.callee.i) }).i = i;}data[0]();data[1]();data[2]();// 0// 1// 2
接下來講講 arguments 對象的幾個注意要點:
arguments 和對應參數的綁定
function foo(name, age, sex, hobbit) { console.log(name, arguments[0]); // name name // 改變形參 name = 'new name'; console.log(name, arguments[0]); // new name new name // 改變arguments arguments[1] = 'new age'; console.log(age, arguments[1]); // new age new age // 測試未傳入的是否會綁定 console.log(sex); // undefined sex = 'new sex'; console.log(sex, arguments[2]); // new sex undefined arguments[3] = 'new hobbit'; console.log(hobbit, arguments[3]); // undefined new hobbit}foo('name', 'age')
傳入的參數,實參和 arguments 的值會共享,當沒有傳入時,實參與 arguments 值不會共享
除此之外,以上是在非嚴格模式下,如果是在嚴格模式下,實參和 arguments 是不會共享的。
傳遞參數
将參數從一個函數傳遞到另一個函數
// 使用 apply 将 foo 的參數傳遞給 barfunction foo() { bar.apply(this, arguments);}function bar(a, b, c) { console.log(a, b, c);}foo(1, 2, 3)
強大的ES6
使用ES6的 ... 運算符,我們可以輕松轉成數組。
function func(...arguments) { console.log(arguments); // [1, 2, 3]}func(1, 2, 3);
應用
arguments的應用其實很多,在下個系列,也就是 JavaScript 專題系列中,我們會在 jQuery 的 extend 實作、函數柯裡化、遞歸等場景看見 arguments 的身影。這篇文章就不具體展開了。
如果要總結這些場景的話,暫時能想到的包括:
- 參數不定長
- 函數柯裡化
- 遞歸調用
- 函數重載...
----------------------轉自冴羽