in usefulFunction, you are expecting the C++ style of pass by reference to affect the original reference to config.updateThis, however, when you call
this.usefulFunction(this.config.updateThis);
You are creating a new reference to the 'false' string (to pass to usefulFunction), and you can't update the original reference in this.config from usefulFunction.
The only way to address this is to pass the name of the object to update. Again, there is no C++ pass by reference in JS. Working example
App = {
config: {
updateThis: 'false'
},
init: function(){
this.usefulFunction(this.config, 'updateThis');
this.consoleIt();
},
usefulFunction: function(object, prop){
object[prop] = 'yeah';
},
consoleIt: function(){
console.log(this.config.updateThis);
}
}
The Problem Is NOT That Strings are immutable
ᚗ̸̢̛͝ claims that the problem is that strings are immutable; however, the problem is deeper than that. The fact that strings are immutable means you can't change the current reference (and therefore have all other references update), but even if they were mutable, you couldn't just set a separate reference and affect existing references
var a = {b:1};
function change(obj) {
// This is assigning {c:2} to obj but not to a
// obj and a both point to the same object, but
// the following statement would simple make obj point to a different object
// In other languages, you could define function(&obj) {}
// That would allow the following statement to do what you intended
obj = {c:2};
}
change(a);
console.log(a); // still {b:1}
this). I have a utility function (in this case,usefulFunction) and I want it to update options I set inside ofconfig.