Galunid
Galunid

Reputation: 578

Javascript extending function which create object

After some research I've never found any "tutorial" about extending function in js. This is not like

var old = some_func;
some_func = function(){ old(); do_some_stuff(); };

but like (I'm gonna show this in java):

class Point{
  protected int x = 0;
  protected int y = 1;
}

class MagicPoint extends Point{
  protected int color = 2;
}

In this code class is my function. I want to achive something like

function Object1(){
  this.a = 0;
}

function Object2(){
  this.b = 1;
}

var Object3 = extend(Object1,Object2);

var abc = new Object3();

abc:
a = 0;
b = 1;

Upvotes: 0

Views: 44

Answers (1)

dReAmEr
dReAmEr

Reputation: 7194

Below should work for you.

Function.prototype.inherits = function(parent) {
 this.prototype = Object.create(parent.prototype);
};

function Object1(){
  this.a = 0;
}

function Object2(){
  this.b = 1;
}

Object3.inherits(Object1);
Object3.inherits(Object2);

function Object3() {
  Object1.apply(this, arguments);Object2.apply(this,arguments);
}

var e = new Object3();

console.log(e.a);
console.log(e.b);

Upvotes: 3

Related Questions