1

I am new to Object Oriented Programming in Javascript. Yesterday I read about prototype and prototype.constructor in javascript and how they play their part in Javascript inheritance.

I have made a javascript function, inherited from one of the parent function. But I want some of the methods of this child not to be inherited by its child functions.

That is,

function A //parent
|
|`callA()
 `makeA()

This is the main parent class.

function B //child of A
|
|` callB() //can be inherited
|` makeB() //cannot be inherited
|` callA() //inherited
 ` makeA() //inherited

B is the child function I wish to write.

function C //child of B
|
|` callC();
|` makeC();
|` callB();
|` callA();
 ` makeA();

I am writing in this following manner:

function A(){}
A.prototype.callA = function()
{
    alert('CallA');
}
A.prototype.makeA = function()
{
    alert('makeA');
}

function B(){
    function makeB(){    //This is not working I am not able to call makeB with B's object
        alert('makeB'); 
    }   
}
B.prototype = new A();
B.prototype.constructor = B;
B.prototype.callB = function()
{
    alert('callB');
}
function C(){}
C.prototype = new C();
C.prototype.constructor = C;
C.prototype.callC = function()
{
    alert('callC');
}
C.prototype.makeC = function()
{
    alert('makeC');
}

$(document).ready(function(){
    var b = new B();
    var c = new C();
    $(document).click(function(){
        c.callB();
        c.callC();
        b.callB();
        b.makeB(); //not being called
    });
});

How to do this part in JS inheritance. Private function for derived object.

hugomg
  • 68,213
  • 24
  • 160
  • 246
Veer Shrivastav
  • 5,434
  • 11
  • 53
  • 83
  • Why do you want to hide `makeB` from `C`? Breaking the Liskov substitution principle like this is a code smell. – hugomg Feb 02 '14 at 05:08
  • 1
    @missingno: You might need it.. I am not sure about `Liskov Substitution`. But in java I have been doing it. You might need some functions which you dont want to share with the child class. Right.? – Veer Shrivastav Feb 02 '14 at 05:37
  • @missingno LSP is overrated. Down with strict subtype relationships and the current OOP "norm"! – user2864740 Feb 02 '14 at 09:49
  • @Veer What about "flattening" the relational tree? Each type can have it's down distinct prototype, separate from the others (i.e. none are "subtypes") - just wire the methods each type should have up manually to an appropriate "meta" function store (or really, the first prototype in which they are defined). – user2864740 Feb 02 '14 at 09:52
  • @Veer That is, the code looks the same as above, but `B.prototype.makeB = function ..` after *removing* the prototype chaining? Then it won't be "inherited" from/to other prototypes. – user2864740 Feb 02 '14 at 09:57
  • The following post should explain how to override and extend functions from Parent classes: http://stackoverflow.com/a/16063711/1641941 – HMR Feb 02 '14 at 10:26

1 Answers1

1

One thing you can do is add the makeB method to your instance as part of the B constructor instead of putting it in the prototype that will be inherited from C:

function B(){
   this.makeB = function(){
     //..
   };
}

However, for this to work you will need to use Object.createe to directly inherit from the parent prototype instead of inheriting from an instance o the parent class:

B.prototype = Object.create(A.prototype);
B.prototype.constructor = B;

Object.create is not supported in IE8 though so you might need to find a shim for it.

http://kangax.github.io/es5-compat-table/#Object.create


That said, I would risk saying that what I suggested is probably not the best way to solve the problem.

The first alternative would be to just make those hidden methods public. Are you sure you want to go through the trouble of hiding them?

The second alternative is to simply use static functions for the private methods:

function makeB(obj){
    obj.callB();
}
hugomg
  • 68,213
  • 24
  • 160
  • 246