美文网首页
js隐藏属性 caller、callee、constructor

js隐藏属性 caller、callee、constructor

作者: pretty_rain | 来源:发表于2019-06-19 10:55 被阅读0次

    本文章简单介绍js中的隐藏属性 caller、callee、constructor、prototype、proto、arguments。其中构造函数的属性有arguments、caller、prototype、proto;对象的属性有proto;原型的属性有constructor、proto; callee是arguments的属性;

    image.png

    caller判断是被哪个函数调用

    function fun(){
            console.log(fun.caller);
        }
        fun();//null
        function fun2(){
            fun();
        }
        fun2();
        /*ƒ fun2(){
         fun();
         }*/
    

    callee是arguments的一个属性 arguments.callee表示当前函数

    function fun(a,b){
            //fun.length与arguments.callee.length是一回事
            a--;
            b--;
            if(a>0){
                //fun(a,b);
                arguments.callee(a,b);
            }
            console.log(1);
        }
        fun(10,10);
    

    constructor是原型的一个属性   对象.constructor = 构造函数名称

    //构造函数 创建的对象
        function Student(name) {
            this.name = name;
        }
        var zhangsan = new Student('张三');
        if (zhangsan.constructor == Student)
            document.write("zhangsan是根据构造函数<strong>Student</strong>创造(实例化)出来的"+"<br />");
    
    
        //字符串对象
        var str = new String("Hi");
        if (str.constructor == String)
            document.write("str是根据构造函数<strong>String</strong>创造(实例化)出来的"+"<br />");
    
    
        var str2 = '1233'
        if (str2.constructor == String)
            document.write("str2是根据构造函数<strong>String</strong>创造(实例化)出来的"+"<br />");
    

    prototype是构造函数的一个属性 这个属性是一个原型对象

    var product = function(){}
    
    /*自动有一个 prototype属性 它是一个对象--- 原型对象*/
    
    product.prototype.buy=function(){}
    
    product.prototype={}
    

    arguments是构造函数的属性,是一个伪数组

        //遍历实参
        function fun(){
            for(var i=0;i<arguments.length;i++){
                console.log(arguments[i]);
            }
        }
        fun("1",3,4,5);
    
        //比较形参和实参
        function fun(a,b){
            return fun.length==arguments.length?true:false;
        }
        console.log(fun(1,2));
        console.log(fun(1,1,1,1));
    

    相关文章

      网友评论

          本文标题:js隐藏属性 caller、callee、constructor

          本文链接:https://www.haomeiwen.com/subject/drjqqctx.html