views:

33

answers:

1

Is there a reason for object functions to be unset or deleted or simply not applied for any reason at all that isn't intentional?

I am maintaining someone elses code and gone through it many times. I use Google Chromes awesome debuger and also TextMate. These help me find the origin of error relatively fast.

The problem I have now is that i have an object: types. This object conatains...types. And these types have functions and other variables attached to them.

For some reason in the middle of the code, this type has been passed by reference millions of times probably. When it comes to a certain part of the code parts of it, seem to have dissapeared. Puff! And it's gone..!

Anyone have a clue (other than it being removed somewhere else earlier in the code, I'm already looking for that)

Example

Right now I am simply adding the functions on the fly. Not liking it though, feel a little out of control of the code:

if(identifier.kind.hasOwnProperty('getWarning')) {
    identifier.kind.getWarning = CLEANROOM.types[kind].getWarning;
}
+1  A: 

No, properties of objects will not mysteriously disappear for no reason -- at least, not barring implementation bugs, which should be easily ruled out by seeing if the same thing happens in IE, Chrome, and Firefox, which each have their own (and very different) implementations of Javascript.

If any of those layers happens indirectly, though, that's another matter. For instance, if at some point something is serializing the object to a JSON string and then reconstituting it, the result will be an object with nearly all of the properties with data bound to them but none of the ones with functions bound to them. But that's not passing a reference around, that's serializing and deserializing.

The same thing could happen if something is making a copy like this:

dest = {};
for (name in src) {
    value = src[name];
    if (typeof value !== "function") {
        dest[name] = value;
    }
}

E.g., something making a data-only copy. It can also happen less obviously, if something does this:

function clone(src) {
    dest = {};
    for (name in src) {
        if (src.hasOwnProperty(name)) {
            dest[name] = src[name];
        }
    }
    return dest;
}

That makes a "shallow" copy of the object, only copying the properties it has set on it, itself, and ignoring any properties it gets from its prototype. Most (but by no means all) of the properties objects inherit from their prototypes tend to be functions, and so the result of that can seem to be a data-only copy. Example:

function Thingy() {
}
Thingy.prototype.foo = function() {
}
var t = new Thingy();
t.bar = 42;
// `t` has a `foo` function bound to it, indirectly through its prototype,
// and a `bar` property with the value 42
var x = clone(t);
// `x` does *not* have a `foo` function, but it does have a `bar` property,

Of course, you can also happily delete properties from objects that refer to functions:

for (name in obj) {
    if (typeof obj[name] === "function" && obj.hasOwnProperty(name)) {
        delete obj[name];
    }
}

But again, that's not implicit, that's explicit. But if it's hidden in a worker function somewhere, it'd be pretty easy to miss.

T.J. Crowder
THanks for the answer. I'm pretty sure that they are not serialized. The thing is though that all other "types" are fine only this one of them i causing problems. And I can't find the reason. The weird thing is that this wasn't a problem before. Can it be copying the object but not the functions?
WmasterJ
@WmasterJ: LOL, I was updating my answer as you made that comment, with an example of copying without copying functions.
T.J. Crowder
@WmasterJ: Something else occurred to me; I've added it. Has to do with naive cloning of objects that missed out their inherited properties.
T.J. Crowder
Not even that. I am finding my self adding functions on the go... :( (example added)
WmasterJ
+2 thanks for all the help. It did give me a much better understanding of what is happening.
WmasterJ