在JavaScript中,当尝试访问对象属性时,为什么父对象突然变得“未定义”?

时间:2014-06-17 19:36:33

标签: javascript jquery object properties undefined

我试图在Javascript / jQuery中检索对象属性的值,并且我遇到了一些奇怪的结果。

包含问题的整个函数有点大,所以这里是一个相关的块:

           case 'armed':

            userlist.sort(function(a,b){
                Aname = a.profile.Security;
                Bname = b.profile.Security;

                // the following line shows list of properties in console
                console.log(a.profile.Security);

                // the following line returns 'a.profile.Security' is undefined error
                console.log(a.profile.Security.carry_firearm);

                     if(typeof Aname != 'undefined' && typeof Bname != 'undefined')
                {
                    return 0;
                }
                else if(typeof Aname == 'undefined' && typeof Bname != 'undefined')
                {
                    return 1;
                }
                return -1;
            });   
            fillTable(userlist);
            break;

问题在于a.profile.Securityb.profile.Security。我想要做的是访问"安全"但我无法弄清楚如何做到这一点。

当我使用console.log(a.profile.Security);时,我在Firebug中获得以下内容:

enter image description here

但是,每当我尝试将任何列出的属性输出到控制台时,我都会这样:

enter image description here

尝试使用不同方法(例如a.profile.Security["carry_firearm"]

)访问属性时发生同样的错误

可能最令人困惑的部分是父对象显示为undefined而不是属性本身。它说a.profile.Security未定义,即使我自己将它输出到控制台也没有问题。

有人可以帮助我,告诉我这里发生了什么,以及我如何抓住carry_firearm价值?

以下是更多代码......

function sortUsers(element)
{
    $('#search').val('');
    var sort = $(element).text().toLowerCase();
    sort = $.trim(sort);
    var order = $(element).attr('order');
    $('.userlist_header img').remove();
    if(sort == 'name' || sort == 'username')
    {
        if(order == 'asc')
        {
            order = 'desc';
            $(element).attr('order','desc');
            $(element).append(' <img src="../../assets/images/sort_arrow_up.png" />');
        }
        else
        {
            order = 'asc';
            $(element).attr('order','asc');
            $(element).append(' <img src="../../assets/images/sort_arrow_down.png" />');
        }
    }    
    $('.userlist_item_wrapper').remove();

    switch(sort)
    {

        case 'armed':

            userlist.sort(function(a,b){
                Aname = a.profile.Security;
                Bname = b.profile.Security;

                if(typeof Aname != 'undefined' && typeof Bname != 'undefined')
                {
                    return 0;
                }
                else if(typeof Aname == 'undefined' && typeof Bname != 'undefined')
                {
                    return 1;
                }
                return -1;
            });   
            fillTable(userlist);
            break;


        case 'name':

            userlist.sort(function(a,b){
                Aname = a.first_name.toLowerCase()+' '+a.last_name.toLowerCase();
                Bname = b.first_name.toLowerCase()+' '+b.last_name.toLowerCase();
                if(Aname == Bname)
                {
                    return 0;
                }
                if(order == 'desc')
                {
                    if(Aname < Bname)
                    {
                        return 1;
                    } 
                }
                else
                {
                    if(Aname > Bname)
                    {
                        return 1;
                    } 
                }
                return -1;
            });

            fillTable(userlist);
            break;
        /* there are more cases after this */

0 个答案:

没有答案
相关问题