function A()
{} function B()
{
B.prototype=new A();
} B.prototype.z=0;
var b=new B();
alert(b.constructor);
B.prototype.z=10;
alert(b.z); //0
b=new B();
alert(b.constructor);
alert(b.z); //10------function A()
{} function B()
{
this.prototype=new A();
} B.prototype.z=0;
var b=new B();
alert(b.constructor);
B.prototype.z=10;
alert(b.z); //10
b=new B();
alert(b.constructor);
alert(b.z); //10
{} function B()
{
B.prototype=new A();
} B.prototype.z=0;
var b=new B();
alert(b.constructor);
B.prototype.z=10;
alert(b.z); //0
b=new B();
alert(b.constructor);
alert(b.z); //10------function A()
{} function B()
{
this.prototype=new A();
} B.prototype.z=0;
var b=new B();
alert(b.constructor);
B.prototype.z=10;
alert(b.z); //10
b=new B();
alert(b.constructor);
alert(b.z); //10
以下属于个人理解.
function A()
{this.x="x"; //用于测试
} function B()
{} B.prototype.z=0;
var b=new B();
//b.z是B对象中z属性的引用
alert(b.z); //0
alert(B.prototype.z);//0
//此时改变B.prototype为对象A,也可以说继承A下的所有属性或方法.
B.prototype=new A();
//证明B已继承了A中的属性
alert(B.prototype.x)// x
alert(b.z);//0
//实际上B的prototype已不存在z属性,但是b中的z还存在.个人觉得现在b已经不在是B对象的引用,也不是A对象的引用.
alert(B.prototype.z);//undefined //即使现在B.prototype.z更改值 注:B.prototype已不存在z,此时实际上为新增
B.prototype.z=10;
//b中的值实际上还是第一次对象引用未更改时的值,即0
alert(b.z) //0//第二次创建对象,实际上现在B中已有两个属性,一个x,一个第二次新增的z,值为10
b=new B();
alert(b.x) //x
alert(b.z) //10
function A()
{} function B()
{
//这里是把B的原型修改成A()
B.prototype=new A();
}
//这里是在B本身的原型中增加z属性
B.prototype.z=0;
//这句把B初始化了,如果再new一下B,B的原型就指向A构造函数了,但对于这句来说B的原型依旧是原来的原型,b和B原始的原型一致
var b=new B();
alert(b.constructor);
//由于已经new过一次B了,现在B的原型指向A了,已经不是B原型本身了
B.prototype.z=10;
//由于b和B原始的原型一致,所以用的就是B.prototype.z=0; 这个了
alert(b.z); //0
//这里重新new了一次,B的原型已经指向A了
b=new B();
alert(b.constructor);
alert(b.z); //10
LZ说的下面用this的时候,我认为this指向的是b,所以才产生那样的结果
function A()
{} function B()
{
//这里是把B的原型修改成A()
B.prototype=new A();
}
//这里是在B本身的原型中增加z属性
B.prototype.z=0;
这句把B初始化了//“这句”是指上面蓝色的那句吗?
----如果是这样,这才是理解问题的关健所在。
//这句把B初始化了,如果再new一下B,B的原型就指向A构造函数了,但对于这句来说B的原型依旧是原来的原型,b和B原始的原型一致
var b=new B();
这句代表下面这句
想想强类型的语言的原理, 当new一个对象后, 在内存中新开辟一块空间储存, 但是当你更改类的时候, 跟这块内存地址无关。 不过也只有JS 才会出现这种情况吧, 呵呵, 其实你在更改B的原型时, 跟b已经没有任何关系了。
function A()
{} function B()
{
B.prototype=new A();
} B.prototype.z=0;
var b=new B();
alert(b.constructor);
B.prototype.z=10;
alert(b.z); //0
b=new B();
alert(b.constructor);
alert(b.z); //10 下面是犀牛指南上的代码
function Complex(real, imaginary) { this.x = real; // The real part of the number this.y = imaginary; // The imaginary part of the number}alert(Complex.constructor)
if(Complex.constructor=Complex)
alert(true)Complex.prototype.magnitude = function( ) { return Math.sqrt(this.x*this.x + this.y*this.y);};
Complex.prototype.negative = function( ) { return new Complex(-this.x, -this.y);};Complex.prototype.toString = function( ) { return "{" + this.x + "," + this.y + "}";};
Complex.prototype.valueOf = function( ) { return this.x; }
Complex.add = function (a, b) { return new Complex(a.x + b.x, a.y + b.y);};
Complex.subtract = function (a, b) { return new Complex(a.x - b.x, a.y - b.y);};
Complex.multiply = function(a, b) { return new Complex(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);};Complex.zero = new Complex(0,0);Complex.one = new Complex(1,0);Complex.i = new Complex(0,1);function MoreComplex(real, imaginary) { this.x = real; this.y = imaginary;}alert(MoreComplex.constructor)
if(MoreComplex.constructor=MoreComplex)
alert(true)
MoreComplex.prototype = new Complex(0,0);
MoreComplex.prototype.swap = function( ) { var tmp = this.x; this.x = this.y; this.y = tmp;}
var b=MoreComplex.prototype.constructor;
alert(b)
if(b=Complex)
alert(true)There is one subtle shortcoming to the subclassing technique shown here. Since we explicitly set MoreComplex.prototype to an object of our own creation, we overwrite the prototype object provided by JavaScript and discard the constructor property we are given. This constructor property, described later in this chapter, is supposed to refer to the constructor function that created the object. A MoreComplex object, however, inherits the constructor property of its superclass, rather than having one of its own. One solution is to set this property explicitly: MoreComplex.prototype.constructor = MoreComplex;
B.prototype.z=0; //[o1.z = 0]
var b=new B(); //新建一个对象[o2] 指向B的老的prototype[o1],同时,b的prototype的的值为[o3]
alert(b.constructor);
B.prototype.z=10; //[o3.z = 10]
alert(b.z); //0 //取[o2.z],o2的prototype为o1
b=new B();//新建一个对象[o4] 指向B的prototype[o3],同时,b的prototype的的值为[o5]
alert(b.constructor);
alert(b.z); //10 //取[o4.z],o4的prototype为o3对第二种,你new的时候,B的prototype不变,只是new出来的新对象prototype的属性变了,
所以你用b.z,找的都是B.prototype的z