I have a hierarchy of JS Objects, like this:
function Obj1(){
this.att1;
this.Obj2Array;
}
function Obj2(){
this.att1;
this.att2;
}
where Obj1 has a reference to an array of Obj2. As you can see Obj1 and Obj2 can have similar attribute names. no guarantee for uniqueness.
I want to get the JSON of Obj1, and I want to exclude some attributes of Obj2. I know that stringify receives a replacer function or array, And I have tried it but there is the following problem:
When I use a replacer function, how can I differentiate between attributes in Obj1 and Obj2, even if they have the same name? my final goal is to have a behavior like Java toString, where each Object gets to make a decision about its attributes:
Obj1.prototype.stringify = function (key, val){
// if own attribute, return val;
// else it is an Obj2 attribute, return Obj2.prototype.stringify(key, val)
}
Obj2.prototype.stringify = function (key, val){
if (key == "att1"){
return "";
} else if (key == "att2"){
return val;
}
}
I suppose I am missing a better solution.