Hello,
I need to write objects in a File, say that I have for instance:
var obj = { num: 1, date: new Date(), arr: ["a", "b"]};
What I use to do is to stringify it first:
var strObj = obj.toSource();
$.writeln(strObj); // ({num:1, date:(new Date(1377424308540)), arr:["a", "b"]})
Which is fine.
But as soon as I add an Object prototype that I need in the script, such as:
Object.prototype.keys = function(obj) { var array, prop; array = new Array(); for (prop in obj) { if (obj.hasOwnProperty(prop)) { array.push(prop); } } return array;};
The output of the toSource() includes that keys function as well.
var strObj = obj.toSource();
$.writeln(strObj);// ({num:1, date:(new Date(1377423260920)), arr:["a", "b"], keys:(function(obj) { var array, prop; array = new Array(); for (prop in obj) { if (obj.hasOwnProperty(prop)) { array.push(prop); } } return array; })})
That's an overhead that I don't need and I'd like to get rid of. But how?
I've tried a different approach:
var Fun = function (num, date, arr) { this.num = num; this.date = date; this.arr = arr;}
fun = new Fun ( 1, new Date(), ["a", "b"] );
$.writeln("fun: " + fun.toSource());// fun: ({num:1, date:(new Date(1377424622575)), arr:["a", "b"], keys:(function(obj) { var array, prop; array = new Array(); for (prop in obj) { if (obj.hasOwnProperty(prop)) { array.push(prop); } } return array; })})
but the result of the toSource() is the very same.
Any hint is appreciated!
Thanks
Davide Barranca
www.davidebarranca.com