298 Function.prototype members

  • arguments: arguments acquisition function, has been abandoned, and now the recommended practice is to use available internal function  argumentsobject to access the function arguments.
    • (Obsolete meaning: has been removed from the Web standard, although some browsers still support it, but it might stop supporting in some future time, please try not to use this feature.)
  • length: length of the acquired parameter
  • name: the name of the function of acquiring this property can not be modified
  • caller: the function is used to get the current call in which the function has been abandoned.
  • constructor: refers to the current constructor, Function
  • call: call a function, again pointing to this
  • apply: call the function, redirected this
  • bind: redirected to this, returns a new function, do not call.

<!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>

Guess you like

Origin www.cnblogs.com/jianjie/p/12334659.html