setCopyMethods() : Sets a flag for whether or not you want to copy an object's methods - OrvilleChomer/orvObjLib GitHub Wiki
By default, if you serialize an object, any methods (with their code) will not be serialized with everything else.
If you want to serialize the methods too, you can use this method before serializing an object to copy the methods as well.
Code Example:
var orvObjLib = new OrvObjLib(); // instantiate library object
var serObj1, serObj2, deserObj1, deserObj2, newObj1, newObj2;
var exitingObj = {};
exitingObj.sampleProperty1 = " I created this code in: ";
exitingObj.sampleProperty2 = 2017;
exitingObj.sampleProperty3 = new Date();
exitingObj.demoMethod = function(sInput) {
var sMsg = "Hello "+sInput+"!\n";
sMsg = sMsg + exitingObj.sampleProperty1 + exitingObj.sampleProperty2 + ".\n";
sMsg = sMsg + "You ran this code at: " + exitingObj.sampleProperty3.toString();
alert(sMsg);
} // end of demoMethod() method
orvObjLib.setCopyMethods(true);
serObj1 = orvObjLib.serializeObj(exitingObj); // copy any of object's methods
orvObjLib.setCopyMethods(false); // switch the flag back!
serObj2 = orvObjLib.serializeObj(exitingObj); // don't copy any of object's methods
deserObj1 = orvObjLib.deserializeObj(serObj1);
deserObj2 = orvObjLib.deserializeObj(serObj2);
newObj1 = deserObj1.output;
newObj2 = deserObj2.output;
newObj1.demoMethod("Orville"); // will run method since it WAS copied over
newObj2.demoMethod("Orville"); // will throw an error because method was NOT copied over!
Back to API List