js笔记

 

Object.prototype.toString = function(){return "this is Object prototype's toString"};
function a(){
    this.a = 'aa';
    this.toString = function(){
        return "a() function";
    };
}
a.prototype.b = 'b';
a.prototype.toString = function(){//重写a的prototype的toString方法,这样a的prototype可以调到它自己的toString方法
    return "a's prototype";
};
function ext(){};
ext.prototype = new a();
ext.prototype.constructor = ext;
var ex = new ext();
console.log(ex.a);
console.log(ex.__proto__);//__proto__是隐藏属性,在这里等同于a.prototype
console.log(ex.__proto__.__proto__);
console.log(ex.__proto__.__proto__.__proto__);
//结果:
//aa
//a() function { a="aa", b="b"}
//a's prototype { b="b"}
//this is Object prototype's toString {}
//可以看出来,ex的原型指向了类a的一个对象,所以ex可以得到a构造器定义的属性及a的prototype里面的属性,原型链最终到了Object

你可能感兴趣的:(prototype,ext)