Remember that javascript problem I was bit***ng about yesterday? I got an enlightment after visiting this page. Actually I was searching for a clone object function. I almost use the code there, but I stumbled upon the “bob and alice question”. If I do bob = alice, what will alice print as message? It’s bob’s message. Holy shit, would have it been a variable reference mess?



Ho ho ho, it was indeed. I was doing this in my code:

obj.metadata.todo_str = this.mTodo[idx][0]; // adds metadata :)
obj.metadata.todo_priority = this.mTodo[idx][1]; // adds metadata :)
obj.metadata.todo_done = this.mTodo[idx][2]; // adds metadata :)
obj.metadata.just_created = true;
obj.metadata.idx = idx;


With Metadata object prototyping below:

function Metadata() {
this.todo_str='';
this.todo_priority= 3;
this.todo_done= false;
this.just_created= true;
this.todo_idx= 0
}

Node.prototype.metadata = function () {
this.todo_str='';
this.todo_priority= 3;
this.todo_done= false;
this.just_created= true;
this.todo_idx= 0
}

Please, for a while, ignores the redundancy I made there.



See, actually I have seen the symptom .. (darn how should I spell “gejala” in english) (”symptoms” correction - thank to Nico) . let’s called this anomali. So, what I always have in my object properties is the same accross all objects. My hunch said this value come from the last assignment for newly created object. The page I mentioned before really tickles me, waking me up, that this may be a variable by reference hoohaa. After I add this code before the metadata properties assignment

obj.metadata = new Metadata;

Everything went smooth. Though I still use a dirty way of extending the Node instead of created a derivation from it, I got my problem solved for sure. Inthis meantime, I don;t want to find other workaround, that kinda of shit will distract me from my curent significant goal.



As always, till the next code ;)

Sphere: Related Content