Quantcast
Channel: Adobe Community : Popular Discussions - Photoshop Scripting
Viewing all articles
Browse latest Browse all 12244

How to filter Prototypes from .toSource() output?

$
0
0

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


Viewing all articles
Browse latest Browse all 12244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>