function Test1(){
Test1.prototype.name="zhang";
}
function Test2(){
}Test2.prototype=Test1.prototype;
var obj2=new Test2();console.log(obj2.name);
console.log(obj2.constructor);将Test2的对象原型指向Test1的对象原型,然后创建Test2的对象实例,为什么能访问constructor,但不能访问name?而下面的又能访问function Test1(){
}
function Test2(){
}
Test1.prototype={
name:"zhang"
};Test2.prototype=Test1.prototype;
var obj2=new Test2();
console.log(obj2.name);
console.log(obj2.constructor);

解决方案 »

  1.   

    因为第一个只有执行了Test1方法后才会在Test1的原型上加上name属性啊 但是你没执行过Test1
      

  2.   

    你在Test1构造函数中为原型添加属性,就要调用Test1函数才能执行 Test1.prototype.name="zhang";另外,对象继承不能 Test2.prototype=Test1.prototype; 这样弄的,
    你这样后面对子类原型添加属性(方法)时会影响到父类的原型。
    应该是 Test2.prototype = Object.create(Test1.prototype); 用Test1原型对象及其属性去创建一个新的对象。