<script>
//定义一个构造函数(子类)
function Class(){
this.a = "a";
}
Class.prototype.b = "b";
Object.prototype.c = "c";
var o = new Class();
document.write(o.a);//实例属性
document.write(o.b);//继承自原型对象
document.write(o.hasOwnProperty("b"));//返回flase,说明b确实继承自原型对象,同时也说明了继承了超类Ojbect的实例方法hasOwnProperty
document.write(o.c);//继承自超类Object的原型对象
document.write(o.hasOwnProperty("c"));//返回flase,说明c确实继承自超类的原型对象
</script>从上边的例子我们可以实例属性和方法可能继承于子类、子类原型、超类和超类原型,下边一个例子我们再来看看它们的继承体系:
<script>
function Class(){
this.a = "a";
this.b = "bb";
}
Class.prototype.b = "b";
//无法修改超类Object,但肯定是先超类再超类原型
//跳过超类测试对超类原型的隐藏
Class.prototype.c = "cc";
Object.prototype.c = "c";
var o = new Class();
document.write(o.b);//实例属性隐藏了子类原型
document.write(o.c);//子类原型隐藏了超类原型
</script>现在可以看出Javascript的继承体系为:超类原型对象→超类(Object)→子类原型对象→子类!
评论(0)
暂无评论。