这个涉及到传值或者是传地址问题,对于Object在赋值操作是传地址。因此在继承时实际是直接操作基类本身的Object。要想子类实例中继承的Object互不干涉有个简单的方法如下(但是这个可能不是你希望的,因为这样实际上可能违背了继承所要达到的目的):
function a()
{
this.obj = new Object();
this.obj.a = 0;
this.number = 1;
}
function b()
{
}
b.prototype = new a();
x = new b();
b.prototype = new a(); //实例化一个之前就加上这么一句
y = new b();
alert("x.obj.a="+x.obj.a);
alert("y.obj.a="+y.obj.a);
x.obj.a = 10;
x.number = 20;
alert(x.obj.a);
alert(y.obj.a);
alert(x.number);
alert(y.number);
function a()
{
this.obj = new Object();
this.obj.a = 0;
this.number = 1;
}
function b()
{
}
b.prototype = new a();
x = new b();
b.prototype = new a(); //实例化一个之前就加上这么一句
y = new b();
alert("x.obj.a="+x.obj.a);
alert("y.obj.a="+y.obj.a);
x.obj.a = 10;
x.number = 20;
alert(x.obj.a);
alert(y.obj.a);
alert(x.number);
alert(y.number);
Object.extend = function(destination, source) {
for (property in source) {
destination[property] = source[property];
}
return destination;
}Object.prototype.extend = function(object) {
return Object.extend.apply(this, [this, object]);
}function class_a()
{
this.alert=function () {
alert("blueDestiny");
}
}
function class_b()
{
this.Alert=function () {
alert("never-online");
}
}
var a=new class_a();
var b=new class_b();
var c=a.extend(b);
c.alert()
c.Alert();
</SCRIPT>
function a()
{
this.propertys = "p";
this.method = function(){alert("ok")}
}function b()
{
this.base = a;
this.base();
}c = new b();
c.method();
_______________________________
没有用prototype来实现继承这样每个实例都有自己各自的数据。