简体   繁体   中英

typeof jQuery.data() buggy?

I'm tracking a mysterious problem in my code. This "bug" happens within an event handler that was bound via jQuery's .delegate() :

my.$table_body.delegate('.wf-dropdown', 'change', function(e){
    parent_row_element = $(e.target).closest('tr')[0];

    if( typeof $.data(parent_row_element, 'changed') == 'undefined' )
        $.data(parent_row_element, 'changed', {});

    $.data(parent_row_element, 'changed').wf_id = e.target.value;
    // .. more code
}); 

You don't need to know where some of those variables are defined and what they are for, the mysterious thing happens on the check for the undefined value. I obviously check if there already is an object attached to the parent_row_element and if not, create it. It seems like typeof $.data(anything) fails in a way I cannot describe really. If a debugger like Firebug is attached, it evaluates to undefined correctly and everything works fine, without debugger, the assignment to .wf_id always throws because the object is undefined, because it was not created.

It works perfectly if I use this statement:

if(!$.data(parent_row_element, 'changed') )
    $.data(parent_row_element, 'changed', {});

That's why I guess that this is a bug. Or do I overlook something here ?

I could reproduce this behavior:

throws an error on click, typeof is used
http://www.jsfiddle.net/5cEVf/

works fine, no typeof
http://www.jsfiddle.net/5cEVf/3/

Upgrade past 1.4.2 and it is fixed.

If you alert the result of the typeof , you get object in 1.4.2.

The problem is the the value is set to null and null is an object.

So the typeof returns 'object' which makes your if fail and since the object is null it also fails the assignment of a new property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM