Hello StackOverflowers,
I'm implementing a LocalStorage system for my web application.
But my pity is that all the objects in the Model abstraction layer have to be serialized.
I am aware that functions don't get serialized when the object is converted to JSON.
I was about to use Option 1 described here. (Which is, creating a 'static' method which returns a brand new object including the functions).
However, the problem is that even the non-function members of the objects don't get converted as well.
For example at divvie.textContent = JSON.stringify( coffee );, divvie.textContent becomes "{}".
Why? What's going on?
Sample code, not my real application but the situation is much the same: Check Coffee
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 This class represents 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;
/**
* @returns {String}
*/
this.getId = function()
{
return id;
};
/**
* @returns {String}
*/
this.getName = function()
{
return name;
};
/**
* @returns {String}
*/
this.getDescription = function()
{
return description;
};
/**
* @param {String} sugarCubeId
* @param {Number} amountOfSweetness
*/
this.addSugarCube = function( sugarCubeId, amountOfSweetness )
{
/// First check if this sugarCube is already in our coffee.
var sugarCubeFound = false;
for( var i = 0; i < sugarCubes.length; i++ )
{
if( sugarCubes[ i ].getId() === sugarCubeId )
{
sugarCubeFound = true;
i = sugarCubes.length;
}
}
if( !sugarCubeFound )
{
/// Oh Sweet! A new sugar cube to add in our coffee!
sugarCubes.push( new SugarCube( sugarCubeId, amountOfSweetness ) );
maxAmountOfSweetness = Math.max( maxAmountOfSweetness, amountOfSweetness );
}
};
/**
* @param {String} sugarCubeId
* @returns {SugarCube}
*/
this.getSugarCube = function( sugarCubeId )
{
for( var i = 0; i < sugarCubes.length; i++ )
{
if( sugarCubes[ i ].getId() === sugarCubeId )
{
return sugarCubes[ i ];
}
}
};
/**
* @returns {Boolean} True when the amount of sugar cubes in this coffee is 1 or more,
* false when not.
*/
this.isSweet = function()
{
if( 0 < sugarCubes.length )
{
return true;
}
return false;
};
}
/**
* @class This class represents a sugar cube
*
* @param {String} id
* @param {Number} amountOfSweetness
* @returns {SugarCube}
*/
function SugarCube( id, amountOfSweetness )
{
/**
* @returns {String}
*/
this.getId = function()
{
return id;
};
/**
* @returns {Number}
*/
this.getAmountOfSweetness = function()
{
return amountOfSweetness;
};
}
PS: You're right about my use of var and this here. I'm raised with Java and C++, so I use "var" as the "private" access modifier and "this" as the "public" access modifier.
– RoestVrijStaal Aug 12 '14 at 12:37