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.

6
  • Why do you want to hide makeB from C? Breaking the Liskov substitution principle like this is a code smell. Commented Feb 2, 2014 at 5: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.? Commented Feb 2, 2014 at 5:37
  • @missingno LSP is overrated. Down with strict subtype relationships and the current OOP "norm"! Commented Feb 2, 2014 at 9: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). Commented Feb 2, 2014 at 9: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. Commented Feb 2, 2014 at 9:57

1 Answer 1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.