Object-oriented parasitic combination of inheritance

Recently the use of object-oriented programming, inheritance and prototype constructors discovered inheritance or a combination of a little small defects, and thus again JS Advanced Object-Oriented Programming Chapter VI looked again and found that a combination of inherited parasitic really good, they share everyone.

function object( o ) {
  function F () {};
  F.prototype = o;
  return new F()
};

function inheritPrototype ( SubType, SupType ) {
  var prototype = object ( supType.prototype );
  prototype.constructor = SubType;
  SubType.prototype = prototype;
};

function SupType ( name ) {
  this.name = name;
  this.friends = [ "a", "b", "c" ];
};

SupType.prototype.sayName = function () {
  alert( this.name );
};

function SubType ( name, age ) {
  SupType.call( this, name );
  
  this.age = age;
};

inheritPrototype( SubType, SupType );

SubType.prototype.sayAge = function () {
  alert(  this.age );
};


Guess you like

Origin blog.csdn.net/Deng_gene/article/details/70167840