I am a C++/Java developer that has been tasked with a javascript project and I cannot seem to figure this issue out.
I am working with a javascript file that defines a namespace by direct assignment as described here
So here is my attempt at an overly simplified example namespace
// testns.js
var ns = { }
ns.val = -1;
ns.setVal = function(newVal) {
this.val = newVal;
}
And here is what I would like to do
//myScript.js
function testNamespaceInstances()
{
var nsInstance1 = ns;
nsInstance1.setVal(1);
var nsInstance2 = ns;
nsInstance2.setVal(2);
console.log("nsInstance1.val: " + nsInstance1.val);
console.log("nsInstance2.val: " + nsInstance2.val);
}
That function will output
nsInstance1.val: 2
nsInstance2.val: 2
Is there anything I can do without modifying the testns.js file that the namespace is declared in so that the function will output
nsInstance1.val: 1
nsInstance2.val: 2
Edit: Added some more details to the example code. I would also like to note I am attempting to simplify a more complex javascript file that I am working with