javaScript中的Arguments對象

1.搞清楚什麼是arguments

「arguments 是一個對應於傳遞給函數的參數的類數組對象。
arguments對象是全部(非箭頭)函數中均可用的局部變量。你能夠使用arguments對象在函數中引用函數的參數。此對象包含傳遞給函數的每一個參數,第一個參數在索引0處。」

首先它是一個類數組對象,typeof arguments結果毫無疑問是"object",注意結果是字符串類型。接下來調用Object.prototype.toString.call(arguments),結果是從未見過的"[object Arguments]"。數組

2.轉換爲數組

1.Array的silce方法函數

Array.prototype.slice.call(arguments)

2.Array.fromthis

let re = Array.from(arguments)

3.拓展運算符spa

let re = [...arguments]

3.從arguments到類數組

類數組必須有length屬性,具備索引屬性,下面結合代碼說明:prototype

let obj = {

            "0": 'a',

            "1": 'b',

            "2": 'c',

            length: 3,

            "push": Array.prototype.push,

            "splice": Array.prototype.splice

        }

obj.push('d')

console.log(obj)

結果爲:
結果
實際執行過程至關於:code

obj[obj.length] = 'd';
obj.length++;

4.筆試題

var length = 10;
function fn(){
    console.log(this.length)
}
var obj = {
    length: 5,
    getF: function(fn) {
        fn();
        arguments[0]();
    }
}
obj.getF(fn);

考察的是arguments和this指向問題,我答的5 1,真實結果爲10 1。第一次寫文章,但願對大家有點幫助。對象

相關文章
相關標籤/搜索