JS继承类相关试题

题目一:

//有关于原型继承的代码如下:
function Person(name) {
   this.name = name;
}
Person.prototype = {
     getName : function() {
    return this.name;
    }
}
function Student(name, id) {
     Person.call(this, name)
    this.id = id;
}

//接下来的一行,我们提供了有三种写法:
//第一种:Student.prototype = Person.prototype;
//第二种:Student.prototype = new Person();
//第三种:Student.prototype = Object.create(Person.prototype);
         Student.prototype.getId = function() {
             return this.id;
         }
//请问:
//1.请简述代码中注释处的三种写法的每一种,是否正确,是否完美.如果都不够完美,那你应该如何写.
//2.除了注释处,代码的其他部分还有值得优化的地方么.
 
My Answer:
1.这三种写法,写法一是错误的,写法二三是正确的。
  对于写法一,只是把Person的原型对象赋值给了Student的原型对象,这样子虽然Student的实例中可以访问到Person的原型上的属性和方法,但是对于不是绑定在原型上的属性,例如Person中的this.name,实例中是不会有这个属性的。所以这并不是继承。只是简单的引用,因为Person.prototype是一个对象,也是引用类型,这样的赋值只是把Person.prototype的引用赋值给了Student.prototype。既然是引用,那两者如果有一方发生改动,另一方必然会受影响。这样new出来的实例之间也是互相影响的。
 对于写法二,确实是正确的写法,这种写法就是组合继承,但是不是完美的写法。因为创建实例的过程中两次调用了父类的构造函数。
 对于写法三,也是正确的写法,这种写法就是寄生式继承。但是不是完美的写法。首先,ES3中没有Object.create()方法,没有做兼容。然后,这种继承如果父类中含有引用类型,如数组,那么继承之后,各个实例中的这个引用类型会互相影响。
相对更完美的写法:
function inheritPrototype(SubType,SuperType){
   var prototype;
   if(typeof Object.create==='function'){
       prototype=Object.create(SuperType.prototype);
   }else{
       funciont W(){};
       W.prototype=SuperType.prototype;
       prototype=new W();
   }
       prototype.constructor=SubType;
Student.prototype=prototype; }
inheritPrototype(Student,Person);

题目二,感觉给出了写法就是可以用来回答题目二的了。不过,还要优化的话,我想应该是这里

Person.prototype = {
     getName : function() {
    return this.name;
    }
}

这里重新定义了原型对象,覆盖了原来的constructor属性,可以把它定义回去。就是这样

Person.prototype = {
     constructor:Person,
     getName : function() {
    return this.name;
    }
}

  

  

原文地址:https://www.cnblogs.com/LuckyWinty/p/5861618.html