0

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;

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Helped a lot. Can you explain what Object1.apply do?
In simple terms,Object1.apply(this,arguments) allows ,the current object(this) which is Object3 to access the function Object1 scope. For more info developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.