William Jiang

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

JavaScript Inheritance examples

JavaScript Inheritance

Here I list some basic JavaScript inheritance examples for quick retrieving. Original reference from Stoyan Stefanov. JavaScript inheritance are *NOT* as easy as PHP. I am afraid this will prevent Node.js from getting further in the server-side.

//1.Very Basicly ineritance - Copy:
function inheritCopy(C, P) {
  C.prototype = new P();
}
// function Parent(){}; function Child(){};
// inheritCopy(Child, Parent); 
//Child's prototype is an instance invocation of Parent.

The Select2.js using this to do extension.

// select2.js, using jQuery.extend(), multi-inheritance:
function clazz(Parent, methods) {
    var F = function () {};
    F.prototype = new Parent;
    F.prototype.constructor = F;
    F.prototype.parent = Parent.prototype;
    F.prototype = $.extend(F.prototype, methods);
    return F;
}
// var AbstractSelect2 = clazz(Object, {...});
// var SingleSelect2 = clazz(AbstractSelect2, {...});
// var MultiSelect2 = clazz(AbstractSelect2, {...});

If not want to get copy from Parent(), just want prototype inheritance, then:

function Parent() {};
function Child(){};
Child.prototype = Parent.prototype; //reference
var c = new Child(); //get all constructors' prototype.

An advanced version which:

  1. not including Parent attrs;
  2. inerit Parent.prototype;
  3. change Child.prototype not effect Parent
// sub-constructor inherit
function extend(Child, Parent) {
  var F = function() {};
  F.prototype = Parent.prototype;
  Child.prototype = new F();
  Child.prototype.constructor = Child;
  Child.uber = Parent.prototype;
}
// function Shape() {}; function TwoDShape() {};
// extend(TwoDShape, Shape);

The following inherit from an object, not from constructor function, and extends its attrs to combine in the second paramter’s hash object.

function objectMixAttrs(o, attrs) {
  var n = null;
  function F() {};
  F.prototype = o;
  n = new F();
  n.uber = o;
  
  for(var i in attrs) {
    n[i] = attrs[i];
  }
  return n;
}
// var fastFood = objectMixAttrs(food, { fast_hash });

A Recursive and deepCopy example:

function deepCopy(p, c) {
  var c = c || {};
  for (var i in p) {
    if(typeof p[i] === 'object') { // Array or Hash?
      c[i] = (p[i].constructor === Array) ? [] : {};
      deepCopy(p[i], c[i]);
    }
    else {
      c[i] = p[i];
    }
  }
  return c;
}
// var parent={...}; var deep = deepCopy(p);
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: