English 中文(简体)
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.hasOwnProperty( 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.

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签