PDA

View Full Version : JavaScript uber-strangeness; need another set of eyes...



Mike Robinson
20 May 2010, 9:16 AM
Now this is totally weird ...

I've got some code that has been working (in other parts of the app) for a very long time, and here it is doing something that makes zero sense to me. The code more-or-less looks like this:
var options = {
...
params: this.params || {}
};
Ext.applyIf(options.params, {
... various stuff here ...
}Now what seems to be happening is that, immediately upon finishing the execution of "applyIf," I find that this.params now has the same value as the (local var...) "options."

Quite literally: before executing the applyIf(), Firebug tells me that "this.params" is exactly what I think it should be. Immediately after it, and even though the designation of this is the same as before, the value of this.params and options.params is magically the same ... but this is not options.

The "various stuff here" is quite unremarkable... mostly literal assignments, and nothing that really "wanders off" anywhere.

Does this stir memories with anyone? Cuz I don't have any spare hair to pull-out over this. :)

Animal
20 May 2010, 9:26 AM
You have to think "references" in Javascript. Assigment statements set a reference.

So



var options = {
params: this.params
}


The "params" property of the "options" object references the same object that the "params" property of "this" is referencing.

So applying into options.params sets properties of that single, shared object.

Mike Robinson
20 May 2010, 11:25 AM
20607