Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private and privileged methods vs prototype methods

In JavaScript I can create private and privileged methods by declaring them in the constructor. By this I have to move them out of the prototype of the object. Then I lose the possibility of inheritance and some performance since every object will have it's own copy of those methods instead of accessing one prototype object.

So my question now is what might be a petter pattern: Make use of private and privileged methods or not. I am no big fan of dangling so I want to avoid this. So what to do?

What are your experiences?

like image 317
tyrondis Avatar asked Jan 30 '11 01:01

tyrondis


People also ask

What is a privileged method?

A privileged method is a method having access to private properties, but at the same time publicly exposing itself (in JavaScript, also due to JavaScript scope and closures). You can delete or replace a privileged method, but you cannot alter its contents.

What is class prototype JavaScript?

What is prototype inheritance in JavaScript? In prototypical inheritance, prototypes are object instances to which child instances delegate undefined properties. In contrast, classes in classical inheritance are type definitions, from which child classes inherit methods and properties during instantiation.

How do you create a private function in JavaScript?

Class fields are public by default, but private class members can be created by using a hash # prefix. The privacy encapsulation of these class features is enforced by JavaScript itself. Private members are not native to the language before this syntax existed.


2 Answers

I've never seen any value in creating so-called 'private' functions in JavaScript. Just mark them somehow to indicate that they're not part of your public API, and so API clients aren't guaranteed that the function will exist, or have the same implementation in a future version.

Apart from API consistency, there's no reason not to let people just use your private functions if they want to. Sure, it allows co-existing scripts to fuddle with your private functions, but those scripts could've already overridden your public API functions, anyway.

The accepted answer to this question has good commentary on this: Private functions in namespaced javascript

like image 93
Jason LeBrun Avatar answered Sep 29 '22 02:09

Jason LeBrun


Convention would be to scope what is needed out... and for psuedo private or protected members to prefix your methods or properties with an underscore.

I prefer a psuedo private/protected scope...

var MyObject = (function(){
  var interalStaticVar;

  function ctor(arg1, arg2) {
    //create psuedo-protected context
    this._ = {};

    //create a psuedo-private context
    this.__ = {};

    //stash someval
    this.__.someVal = "hands off";
  }

  ctor.prototype.getSomethingPrivate = function() {
    return this.__.someVal;
  }

  ctor.prototype._doSomethingProtected = function(){ ... }

  ctor.prototype.__doSomethingPrivate = function() { ... }

  return ctor;
}());

I will say that trying to apply OO-style inheritance paradigms to JavaScript is asking for trouble and probably means you're doing something wrong. I tend to follow more SOLID designs embracing the functional event driven nature of JS in the browser.

like image 35
Tracker1 Avatar answered Sep 29 '22 04:09

Tracker1