How can I change the value of a private variable in a JavaScript method from outside?
2 answers
If you just want to override the value, then using a variable instead of a hard-coded value in the object would do the trick:
var x = 321;
var o = {
p: 123,
m: function(a) {
var v = x;
alert(v);
}
};
At this point, when you call o.m(1);
, you get a warning with the value "321". If you then change the value x
to "456" o.m(1);
will return that value next time.
0
source to share