JavaScript深入之類數組對象與arguments
JavaScript深入系列第十三篇,講解類數組對象與對象之間的相似與差異以及arguments的注意要點
類數組對象
所謂的類數組對象:
擁有一個 length 屬性和若干索引屬性的對象
舉個例子:
var array = ['name', 'age', 'sex'];
var arrayLike = {
0: 'name',
1: 'age',
2: 'sex',
length: 3
}
即便如此,為什么叫做類數組對象呢?
那讓我們從讀寫、獲取長度、遍歷三個方面看看這兩個對象
讀寫
console.log(array[0]); // name
console.log(arrayLike[0]); // name
array[0] = 'new name';
arrayLike[0] = 'new name';
長度
console.log(array.length); // 3
console.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&sex
Array.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. slice
Array.prototype.slice.call(arrayLike); // ["name", "age", "sex"]
// 2. splice
Array.prototype.splice.call(arrayLike, 0); // ["name", "age", "sex"]
// 3. ES6 Array.from
Array.from(arrayLike); // ["name", "age", "sex"]
// 4. apply
Array.prototype.concat.apply([], arguments)
那么為什么會講到類數組對象呢?以及類數組有什么應用嗎?
要說到類數組對象,Arguments對象就是一個類數組對象。在客戶端JavaScript中,一些DOM方法(document.getElementsByTagName())也返回類數組對象。
Arguments對象
接下來重點講講Arguments對象。
Arguments對象只定義在函數體內,包括了函數的參數和其他屬性。
舉個例子:
function foo(name, age, sex) {
console.log(arguments);
}
foo('name', 'age', 'sex')
打印結果如下:
我們可以看到除了類數組的索引屬性和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是不會共享的。
傳遞參數
將參數從一個函數傳遞到另一個函數
function foo() {
bar.apply(this, arguments);
}
function bar(a, b, c) {
// logic
}
強大的ES6
使用ES6的...運算符,我們可以輕松轉成數組。
function func(...arguments) {
console.log(arguments); // [1, 2, 3]
}
func(1, 2, 3);
應用
arguments的應用其實很多,在下個系列,也就是JavaScript專題系列中,我們會在jQuery的extend實現、函數柯里化、遞歸等場景看見arguments的身影。這篇文章就不具體展開了。
如果要總結這些場景的話,暫時能想到的包括:
-
參數不定長
-
函數柯里化
-
遞歸調用
-
函數重載
...
來自:https://segmentfault.com/a/1190000009328344