Private and privileged methods vs prototype methods?

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.

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.

I would add that namespacing is a good idea fore anything meant to be shared in terms of JS. Also, emulating OO-style inheritance is a horrible pattern to try to use in JS. SOLID is probably better, but nothing is perfect.

– Tracker1 Jan 30 at 9:36.

You can emulate private or internal methods and properties by using obj. _foo = private; obj.prototype. _internalMethod; You need to seperate your private methods from inheritance.

Anything that can be used without relying on inheritance will work fine. Also the pattern like : function construct() { var priv; this. SomeValue = foo; this.

SomeMethod = function() { } } Here we ignore the prototype and write to the object directly. This pattern relying on closures to hide methods and variables works well with mixins. Worrying about whether redeclaring methods in the constructor is inefficient is a micro optimisation and evil.

If you don't expect to create at least 1000 objects the difference is neglible.

I'm just going to guess that you have some functions that are used in the constructor, but are not part of the public API. In this case, one option would be to simply store them as a property on the constructor. Then when calling them in the constructor, you'd use the .call() method to set the context of the current object being constructed.

This way you automatically have access to public members via this, and to private variables if you pass them as an argument. Var myClass = function() { this. Someprop = 'prop value'; var privVar = 'private value'; myClass.func.

Call(this, privVar); }; myClass. Func = function(priv){ alert(this. Someprop); alert(priv); }; var inst = new myClass; So this just uses the MyClass function as namespace storage for a function that is used by the constructor.

The constructor calls it from the context of this, which does something with this. SomeProperty and the privVar that is passed in. Not sure if this is what you're after, but it is one option.As correctly noted by @Raynos in the comments below, any properties added to the MyClass constructor as simple namespace storage are not private at all, and can be accessed by any code that can access the constructor.

You could do some checking to help ensure it is called properly, like adding an instanceof check to make sure it is called from an instance of MyClass, but such checks are not at all secure and do not offer the protection of actual private members.

– Raynos Jan 30 at 1:57 1 @Raynos: You've hit the nail on the head. It is likely micro-optimization and certainly doesn't help readability.It's just a pattern that could be used to achieve what OP is (I think) asking. It isn't necessarily good or bad, but whether or not it is worth using will be up to OP I guess.

– user113716 Jan 30 at 2:01 @patrickdw in that case please always go for readability. Your always allowed to hand-optimise real bottlenecks later. – Raynos Jan 30 at 2:03 @Raynos: I understand what you're saying and I'd agree that in most cases it would be better to simply use private functions.

There are times though where it may make sense to avoid the additional overhead, in which case something like this may be worthwhile. Your comments and answer give a good counter perspective to this solution. – user113716 Jan 30 at 2:08 @Patrickdw your right this is a valid alternative but do mention that you can call that method in inherited objects which you couldn't do with private functions.

I.e. Call Super. Func in SubClass – Raynos Jan 30 at 2:15.

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.

You could get more elegant and use this'\0xff' or something else that may sniff badly. – Tracker1 Jan 30 at 9:55.

I think it all depends on how you want to use your objects. If you need private variables/functions in some object, and want them to be accessible by the prototype methods of that object, you have to declare the prototype methods within the constructor (see example here) by the way. In my opinion it boils down to the design and usage of the program.

If you need objects with really private variables or - functions, design it like the example I gave, otherwise don't. In my experience private/privileged functions are not really influencing performance.

(function () { function privateFunc () { ... } A.prototype. PublicFunc1 = function () { ... }; A.prototype. PublicFunc2 = function () { ... }; })(); function A () { ... } I Edit: This doesn't help if you also want private variables.

1 Confusing sample code. Private function doesn't have access to this or private variables declared in A – Raynos Jan 30 at 1:58 If it had access to private variables in another function, they would not be private, would they? I find this comment confusing.

The private function are not methods but you can pass the object if needed. – anm Jan 30 at 2:15 1 The general pattern is private variables and methods in the constructor. The private method in a different closure does not have access to any of the data in A object.

– Raynos Jan 30 at 2:20 As I said, it is not a method, it is a function. Any data it needs is passed to it by the public method. – anm Jan 30 at 2:35 Off topic from this discussion, but don't forget to make your immediately invoked function an expression.

Right now it's a declaration, and as such, it can't be invoked immediately. – user113716 Jan 30 at 2:43.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions