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

解决方案 »

  1.   

    第二个相对好理解,this就是代表的当前对象.b=new B(); 那么b.prototype=new A().第一个代码实际上为以下代码:
    以下属于个人理解.
    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
      

  2.   

    。。 想想强类型的语言的原理, 当new一个对象后, 在内存中新开辟一块空间储存, 但是当你更改类的时候, 跟这块内存地址无关。 不过也只有JS 才会出现这种情况吧, 呵呵, 其实你在更改B的原型时, 跟b已经没有任何关系了。
      

  3.   


    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,所以才产生那样的结果
      

  4.   

    楼所说,
    function A() 
    {} function B() 

    //这里是把B的原型修改成A()
    B.prototype=new A(); 

    //这里是在B本身的原型中增加z属性
    B.prototype.z=0; 
    这句把B初始化了//“这句”是指上面蓝色的那句吗?
    ----如果是这样,这才是理解问题的关健所在。
      

  5.   

    对以上加红的理解:引网友的见解:对象prototype的属性不能通过obj.属性=...去改变,这样做只是给obj用后绑定的方法添加了一个同名属性,prototype里的那个没有变化。变量自己的属性比prototype里的优先级要高,先读取自己的,没有才会顺着prototype链去找
      

  6.   


    //这句把B初始化了,如果再new一下B,B的原型就指向A构造函数了,但对于这句来说B的原型依旧是原来的原型,b和B原始的原型一致
    var b=new B(); 
    这句代表下面这句
      

  7.   

    红、蓝两部分,实际上是矛盾的。z 既然已经没有,到哪里去引用?这样说,有些欠妥, 怎样正确的说,还请大家指教。5楼所说,帮助我们理解这个问题:
    想想强类型的语言的原理, 当new一个对象后, 在内存中新开辟一块空间储存, 但是当你更改类的时候, 跟这块内存地址无关。 不过也只有JS 才会出现这种情况吧, 呵呵, 其实你在更改B的原型时, 跟b已经没有任何关系了。
      

  8.   

    构造函数都有个原型对象,非空,有个constructor属性,即它的构造 函数本身。它的子类原型当然也是这样,有个与自己同名的construcor 属性。如果它的子类原型,强制修改为父类的实例,根据属性查找的原型链,这个设定的原型实例的constructor(父类同名) 掩盖了系统默认的constructor(子类同名),那也就把它的constructor 属性修改为父类的constructor了。这对理解咱们正在讨论的 b 的constructor 及属性大有帮助。
    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; 
      

  9.   

    首先要知道prototype的原理,一个对象new了之后,他会有个原型对象,可以理解为有个引用指向这个对象对于第一种,
    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
      

  10.   

    第二次new 的时候,,b的prototype的的值为[o5]有误吧?还是o3,我以为。