Reputation: 4869
I have a js file X which has a json object in which I want to pass the object to another js file Y and execute according to the values of the object. These two js files are referenced to the same html page.
The json object is something like this
var displayDetails = {
"A" : {
data : "1",
display : "A1"
},
"B" : {
data : "2",
display : "B2"
}
};
After which the json object was parsed
var cachedOptions = JSON.parse(JSON.stringify(displayDetails));
All this are in js file X. So how do I access that cachedOptions or displayDetails on js B? I am new to json so any help is appreciated. Thanks!
Upvotes: 0
Views: 5925
Reputation: 4611
You can create a global namespace
and attach objects and functions to it so it can be used anywhere in your app.
File X:
window.scope = {};
scope.displayDetails = {
"A" : {
data : "1",
display : "A1"
},
"B" : {
data : "2",
display : "B2"
}
};
scope.cachedOptions = JSON.parse(JSON.stringify(scope.displayDetails));
File Y:
/** Use the object from another file. */
console.log(scope.displayDetails.A);
console.log(scope.cachedOptions);
Upvotes: 1
Reputation: 6618
As long as you aren't using name spacing you can simply refer to a variable in script X that is declared in script Y. The HTML page will compile all the script files listed on the page together so 'global' variables will be able to 'see' each other.
Of course, when writing these files individually the code won't make sense, but it will work in the browser.
Upvotes: 1