William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

Parasitic + Combination Inheritance

JavaScript: Parasitic + Combination Inheritance

The Parasitic + Combination Inheritance which I use in my codes:

//Parasitic + Combination Inheritance
// reference:
function object(o) {
  function F(){}
  F.prototype = o;
  return new F();
}
// deal with prototype:
function inheritPrototype(child, parent) {
  function F() {}
  F.prototype = parent.prototype;
  var p = new F();
  p.constructor = child;
  child.prototype = p;
}

function Super(args) {
 //this attrs and methods
}
Super.prototype.say1 = function() {
  //process args...
}

function Sub(args, more_args) {
  //inherit Super
  //2.
  Super.call(this, args);
  this.more_arges = more_args;
}
//1.
inheritPrototype(Sub, Super);

Sub.prototype.say2 = function() {
  // process more_args;
}

These are original from Nicholas Zakas’s ‘Professional JavaScript for Web Developers (3rd Edition)’ which I refer to.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: