298 Function.prototype成员

  • arguments:获取函数的实参,已经被废弃了,现在推荐的做法是使用函数内部可用的 arguments对象来访问函数的实参。
    • (废弃的意思:已经从 Web 标准中删除,虽然一些浏览器目前仍然支持它,但也许会在未来的某个时间停止支持,请尽量不要使用该特性。 )
  • length:获取形参的长度
  • name:获取函数的名字,此属性不允许修改
  • caller: 用于获取当前在函数是在哪个函数中调用的,已经被废弃了。
  • constructor:指向当前构造函数,Function
  • call:调用函数,重新指向this
  • apply:调用函数,重新指向this
  • bind:重新指向this,返回一个新的函数,不调用。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        // 介绍Function.prototype原型成员

        // 奇葩:在js中函数是一等公民(特殊)
        console.dir(Function.prototype);  // 是个函数类型

        // call  apply  bind

        // 1. arguments 是 Function.prototype原型中的一个属性
        //  废弃:标准中已经删除了该属性,但是有些浏览器可能还支持

        /*function fn(){
            console.log(fn.arguments);  // 废弃,老版本的写法

            // arguments  内置对象
            console.log(arguments); // 推荐的写法来获取实参列表
        }
        fn(10, 20, 30);*/




        // 2.caller  废弃,可以获取函数是在哪个函数内被调用的。
        /*function fn(){
            function foo(){
                console.log(foo.caller);
            }
            foo();
        }
        fn();*/




        // 3.length 获取形参的个数
        /*function fn(a, b, c, d){

        }
        console.log(fn.length);*/



        /*function sum(n1, n2 ,n3){
            // 需求: 只有在实参的个数等于形参的个数的时候,才执行打印

            if(arguments.length != sum.length){
                return;
            }

            console.log(n1 + n2 + n3);
        }
        sum(10, 20, 40)*/




        // name: 可以用来获取函数名
        //  name 属性是只读的属性
        /*function fn2(){

        }
        fn2.name = "fn3";
        console.log(fn2.name);  // fn2*/




        // toString  转字符串
        function fn3(){
            console.log(1);
        }
        console.log(fn3.toString());



    </script>
</body>
</html>
原文地址:https://www.cnblogs.com/jianjie/p/12334659.html