javascript中函数构造器和原型研究

来源:互联网 发布:玉溪广电网络分公司 编辑:程序博客网 时间:2024/06/01 07:19

    var ParentObj = function(){
        this.say = function(){
            console.log('parent');
        }
    };

    var ChildObj = function(){
        this.say = function(){
            console.log('child');
        };
    };

    //ChildObj.prototype 不等于 ChildObj or Object
    //ChildObj.prototype.constructor === ChildObj   这个是对的
    //console.log(ChildObj.prototype == new Object);

    //执行delete ChildObj.prototype.constructor ;之后,ChildObj.prototype.constructor == Object为真,在类创建以后,prototype指向的是Object的一个实例,但是constructor赋值为当前对象,当使用delete删除以后,就会使constructor指向Object
    //因此函数和构造器的区别在于prototype属相是否指向一个有意义的值,其他的没有本质区别
    //console.log(ChildObj.prototype.constructor == Object);
    //ChildObj.prototype = new ParentObj();
    var p = new ParentObj();
    var c = new ChildObj();
    //在初始的状态下,p.constructor == ParentObj 为true对象实例的constructor指向构造器
    //在ChildObj.prototype = new ParentObj();的情况下p.constructor == c.constructor为true
    console.log(p.constructor == c.constructor );

原创粉丝点击