We all know you can store data in JSON files, like this:
{
data: "this is the data",
}
then make an AJAX request or use fetch
to retrive it:
fetch("data.json").then(data => {
data = data.json();
console.log(data);
});
But, you can use JavaScript modules to store data aswell:
export default {
data: "this is the data",
};
then import
it:
import data from "./data.js";
console.log(data);
Storing data in modules is more dynamic, as you can use variables, functions, and all other features of JS. Additionally, it seems easier handle the information with import
rather than fetch
or an AJAX request. There is also a dynamic import that can be used similar to fetch
.
Possibly the greatest advantage of using modules is that you can leave comments.
Despite this, I never see people use JS modules for this purpose, but instead see people storing data in JSON files. Why is this, and what are the advantages of using JSON files to store information over JS modules?
question from:
https://stackoverflow.com/questions/65661606/advantages-of-using-json-files-over-js-modules-to-store-data 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…