typeof 和constructor

typeof可以检查到变量是否有定义,而construct只能检查已定义变量的类型。

construct检测返回:

Object / Array / Function / String / Number / Boolean

    var i ;
    console.log(typeof i);           //undefined
    console.log(i.constructor.name); //erroe
    var i=2 ;
    console.log(typeof i);                     //number
    console.log(i.constructor.name);             //Number
    console.log(typeof (typeof i));           //string
    console.log(typeof (i.constructor.name)); //string
    console.log(typeof (i.constructor));      //function
    function A(){
        //this.prototype = true ;    
        this.color = [1,21]
    };
    A.prototype.getValue = function(){
        return this.prototype ;    
    };
    function B(){
        this.Bprototype = false ;    
    };
    
    B.prototype = new A();
    B.prototype.getValue = function(){
        return false ;    
    };
    var BB = new B();
    BB.color.push(33);
    var AA = new A();
    var BBB = new B();

    console.log(typeof AA);                     //object
    console.log(AA.constructor.name);             //A
    console.log(BB.constructor.name);             //A
    console.log(typeof (AA.constructor));      //function

 

 

posted on 2016-06-15 10:43  鸢尾。  阅读(105)  评论(0)    收藏  举报

导航