I understand that prototype method is shared by all instances of an object in JavaScript.
I can't think of a practical example when you will need the other situation—declaring the method inside the constructor function and thus making every instance of the object having it's own instance of the method. Can you provide a case for this?
The primary use is emulating private fields. See my answer here for an example:
Declaring javascript object method in constructor function vs. in prototype
eval
hacks in certain browsers that have proprietary features. There are always workarounds to manipulate closure state externally. Using internal properties like this._prop
has the same benefit of letting your user know that the state of that property should not be alternated - Raynos 2012-04-05 16:34
eval
hacks is very, very different to being available via a public property - Tim Down 2012-04-05 16:36
eval
hacks. Keeping the properties on my object down to just those that are designed to be manipulated is the ideal: it makes the object easier to inspect in standard tools and it protects it from inadvertent breakage. Do you disagree that those are good things? I acknowledge that it comes at a cost, hence my comment about it being a trade-off - Tim Down 2012-04-05 16:55
but I can't think of a practical example when you will need the other situation
That's because there is no practical example.
Declaring functions inside the constructor is a known bad practice due to incurring unnecessary performance penalties.
It should also be noted that prototypes are awesome because they encourage extensibility, flexibility and monkey patching. Meaning that you can fix someone else's objects because everything can be intercepted and manipulated.
Closures are like frozen objects, they remove flexibility from you and are a nightmare to manipulate, wrap or alter.
It should be noted you don't need to use prototypes and can use functions instead if that pleases you
function cake(fruits, chocolate, size) {
return {
slice: function () {
return cakeSliceList(this)
},
toString: function () {
return "A lovely cake containing " + fruits.toString()
+ ", " + chocolate.toString()
},
weight: function () {
return size * CAKE_SIZE + fruits.weight() + chocolate.weight()
}
}
}
A functional style is valid, combining the functional style together with prototypes get's rather silly, rather quickly.
private
properties has a value, and using them in PHP is not "wrong". There is a value to doing it in javascript, -even if you're only doing it out of personal preference - Chris Baker 2012-04-05 16:30
drawInterface
method in the public API, even though I am never going to invoke it externally because I know not to. I make it private through constructor closure, now I don't see it in the public interface of that object. That, to me, is of practical value - Chris Baker 2012-04-05 16:35
_
Raynos 2012-04-05 16:37
I agree with @Raynos to an extent - it does feel like an unnecessary and futile practice to implement private state at the cost of efficiency.
In general it’s necessary to create functions when any sort of state-binding is required. So it could be of practical value when you need to pass functions around that refer explicitly to the object state. If you’re going to incur the penalty each time you create a state-bound function then you are at a plus if you bind it just once in the constructor and reference that single bound function rather than creating it multiple times.
State binding is sometimes necessary by design choices out of your own hands. It would make sense for example to create state-bound event handlers for an object on initialisation.
Function#bind
can be used effectively to bind methods from the prototype to the instance rather then using closures - Raynos 2012-04-05 17:01
If you are using closures anywhere in your object, and need to reference them inside your function. Or, alternatively, if you specifically want to make your function "private".
Of course, this begs the question of under what circumstances you want to use closured variables. The 'private' argument may popup here again. Additionally, here is a discussion as to some common uses of closures.