javascript - JS: revealing module pattern - accessing internal objects vs arrays? -
using revealing module pattern, how can provide direct access non-static private variables? here's have:
var m = function () { var obj = {}; var arr = []; var change = function () { obj = {"key":"if see this, o reference obj"}; arr.push("if see this, reference arr") }; return { change: change, o: obj, a: arr }; }(); m.change(); console.log(m.a); // prints ["if see this, reference arr"] console.log(m.o); // prints object {}, wanted "if see this, o..."
it seems references arr directly, while o settles copy of obj's value @ initialization time. understand behavior if obj string, float, or boolean.
i of course expose obj via public get_obj method, i'm still curious if can solved without additional methods (i want keep interface obj intact). furthermore, what's special arrays objects don't have, causes behavior?
really grateful insights,
obj["key"] = "if see this, o reference obj";
you can set key
property obj
, keep reference original object.
Comments
Post a Comment