function modify(val, newVal) {
val = newVal;
}
constructorFunc = function () {
var _private = false;
return {
modifyPrivate: function(toVal) {
return modify(_private, toVal); // LINE REFERRED TO BELOW AS X
}
};
}
var x = constructorFunc();
x.modifyPrivate(true);
x.modifyPrivate(true); // _private still starts off as false, meaning it wasn't set to true
The one question I have is why the second time I call x.modifyPrivate(true) why is it that when line X is run, the value of _private passed in is still ‘false’.
I can make sense of this if I modify my knowledge of closures slightly to be that the closure is done by reference, and when you change the value of a reference you are not chaning the value the original reference pointed to, you are changing the reference itself to point to some new value… But this whole thing is very confusing and I’m sure someone out there can point me to a diagram on the net that explains this.
I’m also very interested in knowing how to write this code so that _private is in fact modified for the subsequent calls to modify().
JavaScript always passes by value, so there’s no way to pass a variable to a function and have the function assign a new value to it as you tried to do.
The
modifyfunction is actually not needed at all. Just do this:The method
modifyPrivatehas access to the private_private, since it was defined in an inner scope. It’s returningthisas suggested by raina77ow, so you can chain another method call ofxif you wish (e.g.x.modifyPrivate(true).foo(), if you definefoolike you did formodifyPrivate).Now, if you really need to modify the value from an external function that doesn’t have access to that scope, you can wrap you private value in an object, and pass the object. In this case, the passed value will be a reference to the object, so modifying its properties would work (note that you can’t reassign to the object, just manipulate the properties):