Simple inheritance via jQuery.extend

Spoiler warning:

This is not true inheritance as we are used have to in static languages. It is not even prototypal inheritance. It is merely an experiment to find out if it is possible to achieve (mock) inheritance using jQuery.extend() method. And yes, it is kind of possible.

JQuery.extend basically copies properties from one object onto another. So in order to mock inheritance you have to extend instance (or instances) of ‘parent class’ with properties of another objects.

Here is how I did it:

$(function() {
  var Parent = function() {
    var that = this;
    this.name = 'david';
    this.getName = function() {
       return 'name is: ' + that.name;	 };
  };
  var AnotherParent = function() {
     this.doSomething = function() {
       return 'I am a test...';	 }
  };
  var SomeStaticClass = {
     staticVariable: 'I am static'
     , staticFunction: function() {	return 'some static value or whatever' }
  };

  var result = $.extend(
    true
    //read properties from instance not from function definition
    , new Parent()
    , new AnotherParent()
    , SomeStaticClass
    //read properties from some anonymous object -> overwrite properties from Parent
    , {tryIt: 'try it', getName: function() {return 'Name also is: ' + this.name}}
    );

  console.log(result);
});

This is only simple case. If you are building a complex project and need inheritance, then it is probably better to use Dojo framework.