1、javascript中的每个引用类型(原生的、和自定义的)都有prototype属性,Javascript中对象的prototype属性的解释是:返回对象类型原型的引用。
A.prototype = new B();
理解prototype不应把它和继承混淆。A的prototype为B的一个实例,可以理解A将B中的方法和属性全部克隆了一遍。A能使用B的方 法和属性。这里强调的是克隆而不是继承。可以出现这种情况:A的prototype是B的实例,同时B的prototype也是A的实例。
1 if (true) { 2 function baseClass() 3 { 4 this.showMsg = function() 5 { 6 alert("baseClass::showMsg"); 7 } 8 } 9 10 function extendClass() 11 { 12 this.hello= function(){alert("子类");} 13 } 14 15 extendClass.prototype = new baseClass(); 16 var instance = new extendClass(); 17 instance.showMsg(); 18 baseClass.prototype=new extendClass(); 19 var base=new baseClass(); 20 base.hello(); 21 2 if (true) {
function baseClass()
{
this.showMsg = function()
{
alert("baseClass::showMsg");
}
}
function extendClass()
{
this.hello= function(){alert("子类");}
}
extendClass.prototype = new baseClass();
var instance = new extendClass();
instance.showMsg();
baseClass.prototype=new extendClass();
var base=new baseClass();
base.hello();
} if (true) {
function baseClass()
{
this.showMsg = function()
{
alert("baseClass::showMsg");
}
}
function extendClass()
{
this.hello= function(){alert("子类");}
}
extendClass.prototype = new baseClass();
var instance = new extendClass();
instance.showMsg();
baseClass.prototype=new extendClass();
var base=new baseClass();
base.hello();
} if (true) {
function baseClass()
{
this.showMsg = function()
{
alert("baseClass::showMsg");
}
}
function extendClass()
{
this.hello= function(){alert("子类");}
}
extendClass.prototype = new baseClass();
var instance = new extendClass();
instance.showMsg();
baseClass.prototype=new extendClass();
var base=new baseClass();
base.hello();
}
function baseClass()
{
this.showMsg = function()
{
alert("baseClass::showMsg");
}
}
function extendClass()
{
this.hello= function(){alert("子类");}
}
extendClass.prototype = new baseClass();
var instance = new extendClass();
instance.showMsg();
baseClass.prototype=new extendClass();
var base=new baseClass();
base.hello();
}