Disappearing object function?
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 debugger 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 contains...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 disappeared. 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.h开发者_如何学GoasOwnProperty('getWarning')) {
identifier.kind.getWarning = CLEANROOM.types[kind].getWarning;
}
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.
精彩评论