Hello fellow developers,
I'm currently working on setting up a LocalStorage feature for my web application.
One issue I've come across is that all objects in the Model abstraction layer need to be serialized.
I understand that functions aren't included in the JSON conversion of an object.
I was considering using the first option mentioned here, which involves creating a 'static' method to return a new object with functions.
However, even the non-function members of the objects are not being converted properly.
For instance, when doing divvie.textContent = JSON.stringify(coffee);, divvie.textContent ends up as "{}".
Why is this happening? What am I missing?
Here's some sample code similar to what I'm experiencing:
var coffee = new Coffee("0001", "DE Red Coarse", "Douwe Egberts, red & coarse grounded.");
coffee.addSugarCube("0x0F_BROWN", "15");
coffee.addSugarCube("0x0C_WHITE", "12");
var divvie = document.getElementById("run");
divvie.textContent = JSON.stringify(coffee);
</script>
</body>
</html>
/**
* @class A representation of a coffee
*
* @param {String} id
* @param {String} name
* @param {String} description
* @returns {Coffee}
*/
function Coffee(id, name, description) {
var sugarCubes = new Array();
var maxAmountOfSweetness = 0;
// Rest of the Coffee class methods...
}
/**
* @class A representation of a sugar cube
*
* @param {String} id
* @param {Number} amountOfSweetness
* @returns {SugarCube}
*/
function SugarCube(id, amountOfSweetness) {
// SugarCube class methods...
}