JavaScript程序开发(十五)—函数的属性和方法

在函数内部,有两个特殊的对象—arguments和this。arguments主要是保存函数的参数,但是这个对象还有一个叫callee的属性,该属性是一个指针,指向拥有这个arguments对象的函数。

<script type="text/javascript">

    function factorail(num){
        if(num <= 1){
            return 1;
        }else{
            return num * factorail(num - 1);
        }
    }
    document.write(factorail(5)+"<br />");
    function factorail2(num){
        if(num <= 1){
            return 1;
        }else{
            return num * arguments.callee(num - 1);
        }
    }
    document.write(factorail2(5));
</script>

我们已经知道,函数就是对象,因此,函数也有属性和方法。每个函数都包含两个默认的属性,length和prototype。length表示函数希望接收的命名参数的个数。prototype将在后面博文中详细介绍。

<script type="text/javascript">
    function sayName(name){
        alert(name);
    }
    function sum(a,b){
        alert(a+b);
    }
    function sayHi(){
        alert("Hi");
    }
    
    alert(sayName.length);
    alert(sum.length);
    alert(sayHi.length);
</script>
原文地址:https://www.cnblogs.com/yansj1997/p/2563864.html