javascript Arguments對象之length、callee屬性

一、length屬性,咱們能夠利用Arguments對象的length屬性來獲取實際傳遞進的參數有幾個。閉包

function a(x, y, z) {函數

        //arguments.callee指向函數a(),arguments.callee.length==a.length;對象

        alert(arguments.callee.length); 遞歸

        //輸出5io

        alert(arguments.length);function

        if (arguments.callee.length != arguments.length) {call

            //判斷形參與實參個數是否相等,不相等則不執行arguments

            return false;return

        }參數

        alert("執行");

    };

    a(1,2,3,4,5);

二、callee屬性,Arguments對象的callee屬性,指向當前調用的函數,能夠利用它來進行函數自身的重載。在閉包中應用的也比較普遍。

var i = 0;

    function b(num) {

        if (num < 10) {

            num++;

            i++;

            //若是有參數,callee也要把參數帶上;

            arguments.callee(num);

        } else {

           //輸出2次

           alert("調用了"+i+"次callee!");

        }

    }

    b(8);

 Arguments.callee在閉包中的應用,它提供了一種遞歸調調用的功能。

//用arguments.callee計算10的階乘,例如: 1×2×3×4×5×6×7....

    function c(x) {

        return x > 1 ? x * arguments.callee(x - 1) : 1

    } (10);

    //輸出6

    alert(c(3));

    //輸出3628800

    alert(c(10));

相關文章
相關標籤/搜索