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?
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 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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With