3

I have a class

function A()
{
   var that = this;
   var b = new B(); 
   this.setBSize = function(newSize)
   {
      b.setSize(newSize);
   }


};

function B()
{
   var that = this;

   this.setSize = function(newSize)
   {
   ...
   }
}

a = new A();
a.setBSize(5);

How do I avoid writing the setBSize method? How do I expose the public methods of b automatically? I want to do the call like this

a.setSize(5);

Also I need a reference to new B(); which is b inside A()

  • Maybe the following answer will be helpful: http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR Aug 11 '14 at 11:36

5 Answers5

3

You could always set the prototype of A to B if you want to inherit all the methods in B

function A() {
    var that = this;
};

function B() {
    var that = this;

    this.setSize = function (newSize) {
        console.log(newSize); // 5
    }
}

A.prototype = new B();

a = new A();
a.setSize(5);

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • To avoid invoking the constructor you can use `Object.create`. `A.prototype = new B();` -> `A.prototype = Object.create(B.prototype);` which can help when you have an abstract base class. – Matt Clarkson Aug 11 '14 at 09:32
2

In jQuery: $.extend(that, new B()); In angular: angular.extend(that, new B());

function A()
{
   var that = this;
   $.extend(that, new B());
};

function B()
{
   var that = this;

   this.setSize = function(newSize)
   {
   ...
   }
}

a = new A();
a.setSize(5);

And if you want to use any private variables in B() class define them as var someVar, and all public (overridable) variables as that.somePublicVar

Umidbek
  • 1,504
  • 12
  • 26
1

You can use call method for this:

function A() {
    var that = this;
    B.call(this);
};

function B() {
    var that = this;
    this.setSize = function (newSize) {
        this.size = newSize;
    }
}

var a = new A();
a.setSize(5);

Basically you invoke B in context of A, and what happens is that all own properties of the B instance are going to be assigned to this which is A instance. This pattern is called constructor or methods borrowing.

dfsq
  • 191,768
  • 25
  • 236
  • 258
1

You should utilize prototyping.

make a constructor which shares the function among all the classes(objects):

var myConstructor = function(newSize){

   this.setSize = function(newSize)
   {
   ...
   }
}

Now you do instanciation:

var a = new myConstructor(someSize);

var b = new myConstrucotr(someSize);

Now with this change a.setSize() is the same as b.setSize()

Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105
1

Using prototype for inheriting the method setSize and discarding all the this and that code.

function B() {
};
function A() {
    B.call(this);
};

B.prototype.setSize = function(newSize) {
    console.log(newSize);
}

A.prototype = Object.create(B.prototype);
A.prototype.constructor = A;
var a = new A();
a.setSize(5);               // 5
console.log(a instanceof A);// true
console.log(a instanceof B);// true
Barun
  • 1,520
  • 2
  • 12
  • 18