Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Calling private method from prototype method

Tags:

I am sure that this must be a pretty common question but after scouring the internets for several hours, I have not found an answer. Here is the question:

Suppose that I have an interface called mammal. Every Mammal has to be able to sleep and eat. (In the future I may throw exceptions for the Mammal class to force children to implement the function).

function Mammal() {};

Mammal.prototype.eat = function() {};

Mammal.prototype.sleep = function() {};

Now suppose that I have a Dog class who implements the Mammal class:

function Dog() {};

Dog.prototype = new Mammal();

Dog.prototype.eat = function() {
    ...
};

Dog.prototype.sleep = function() {
    ... 
};

The dog's eat function is very complicated and I would like to use a helper function. I have not been able to figure out what the best way to do this is. Here are the points of consideration:

  • The helper function should never be called from outside of the dog class so ideally it should be private.
  • The eat function does not have access to private functions (prototypes do not have access to private functions)
  • I could put the helper function into a privalaged function but:
    • This would still be a public function -> ie: everyone has the right to call it
    • It would not be part of the prototype so every dog would need to have its own helper function. If there were lots of dogs this seems inefficient.
  • I cannot make the eat function a privaliged function because in order for prototype inheritance to work it needs to be part of the prototype.

Question: How can I call a private function from a prototype function? Or more generally: When an object (child object) inherits from another object (parent object) how should children methods use helper functions and is it possible to make these private?

like image 974
sixtyfootersdude Avatar asked Dec 20 '11 19:12

sixtyfootersdude


People also ask

Can we access private method from outside class JavaScript?

Private static methods Like private static fields, they are only accessible from inside the class declaration.

Can we call private method from constructor?

It's ok to call private methods from your constructor to initialize some data that used inside the class. Just be sure that your methods have no "side-effects" like long-time methods that user of your class would probably not expect with just calling your constructor.

Can we call private method from outside class?

We can call the private method of a class from another class in Java (which are defined using the private access modifier in Java). We can do this by changing the runtime behavior of the class by using some predefined methods of Java. For accessing private method of different class we will use Reflection API.


2 Answers

Define your Dog "class" in a closure. Then you can have shared priveliged functions. Just know you will have to be careful about this binding properly when you call it.

var Dog = (function() {
  function Dog() {};

  // Common shared private function, available only to Dog.
  var privateHelper = function() { ... };

  Dog.prototype = new Mammal();

  Dog.prototype.eat = function() {
    privateHelper()
    // Or this if the helper needs to access the instance.
    // privateHelper.call(this);
    ...
  };

  return Dog;
})();

A function on a prototype is still just a function. So follows the same scope and closure access rules as any another function. So if you define your entire Dog constructor and prototype and a secluded island of scope that is the self executing function, you are welcome to share as much as you like without exposing or polluting the public scope.

This is exactly how CoffeeScript classes compile down to JS.

like image 54
Alex Wayne Avatar answered Sep 20 '22 02:09

Alex Wayne


It's not possible. If you need access to private variables/functions, you must use privileged (helper) functions.

It sometimes is possible to use helper functions in a local closure scope instead of private helper functions (in the constructor scope), which would be accessible from prototype functions. See Alex Wayne's or Raynos' answers for that.

You said:

I cannot make the eat function a privaliged function because in order for prototype inheritance to work it needs to be part of the prototype.

Why not?

MammalPrototype = {
    eat: function() {},
    sleep: function() {}
};

function Dog(color) {
    ...
    this.eat = function() {
        ... // using private functions
        MammalPrototype.eat.call(this, options) // ?
    };
};
Dog.prototype = Object.create(MammalPrototype); // inherit from an object

new Mammal() is OK when Mammal really is only a empty function. See https://stackoverflow.com/a/5199135/1048572 for how this works. The shim proposed there is of course not what a native implementation does, but it is exactly what we need here. Do not create a instance just to inherit from it!

Dog.prototype.sleep = function() {
    ... 
};

function Dalmatian() {
    Dog.call(this, "black.white");
    ...
}
Dalmatian.prototype = Object.create(Dog.prototype); // sic
Dalmatian.prototype.xyz = function() {};

To call the super constructor on this means to receive all privileged methods, and their functionality. You will need to do this if you are using private variables and/or functions in "classes" you inherit from, otherwise all calls to inherited privileged functions will affect only the one instance you've set your prototype to.

like image 39
Bergi Avatar answered Sep 21 '22 02:09

Bergi