I have an object which have an array to hold all the children of this object, the children are also instances of the same object (I need this for a tree like structure where the object is a node of the tree)
var bugObject = function(kFlag){
this._kFlag = kFlag;
this._children = []
}
bugObject.prototype.getKFlag = function(){
return this._kFlag;
};
bugObject.prototype.setChildrenFromData = function(data){
var i = 0;
var kFlag = {flagType : 'someFlag', flagValue : -1};
kddFlag.flagType = data.flagType;
var len = data.flagValues.length;
for( i = 0 ; i < len ; i++){
kFlag.flagValue = data.flagValues[i];
this._children.push(
new bugObject(kFlag)
);
//this is just to print the children
for(j = 0; j<=i; j++){
console.log('child : ' + j + ' for test :' + i);
console.log(this._children[i].getKFlag());
}
console.log('--------------------');
}
};
The idea is to create the children of this object based on some data using the setChildrenFromData method here is how I am doing this:
function main(){
console.log('main is called');
var data = {"flagType":"someFlag","flagValues":[0,0,0,0,0,0,0,0,1,0,0]};
var rootNode = new bugObject(null);
rootNode.setChildrenFromData(data);
}
main();
The problem is that instead of getting 11 objects each of them have one of these flags [0,0,0,0,0,0,0,0,0,0,1] I get 11 objects all of them have the flag 1, (the last one)!
Could you please see what is wrong!
Thanks
this(new bugObject(kddFlag));to be the problem.