Javascript:调用对象属性表示对象(用对象文字声明)是未定义的

时间:2013-06-28 06:30:27

标签: javascript-objects

HTML:

<div class="odf_details" id="con_details_nttn_odf_details">
                    <span class="hidden_id">con_details_nttn_odf_details</span>
                    <span class="msg"></span>
                    <table border="1" cellspacing=0 cellpadding=2 class="dtable" align="center" width="100%">
                        <tr>
                            <th>NTTN ODF:</th>
                            <th>Tray</th>
                            <th>Port</th>
                            <th>Circuit Type</th>
                        </tr>
                        <tr class="odf_data_tr">
                            <td class="odf_name_td">
                                <input class="odf_name" type="text" name="nttn_odf_name" disabled="disabled" />
                            </td>
                            <td class="odf_tray_td">
                                <select class="odf_tray" name="con_details_nttn_odf_tray">
                                    <option value="">Please Select</option>
                                    <option value="a" <?php if ($services['con_details_nttn_odf_tray'] == 'a') echo 'selected'; ?> >A</option>
                                    <option value="b" <?php if ($services['con_details_nttn_odf_tray'] == 'b') echo 'selected'; ?> >B</option>
                                    <option value="c" <?php if ($services['con_details_nttn_odf_tray'] == 'c') echo 'selected'; ?> >C</option>
                                    <option value="c" <?php if ($services['con_details_nttn_odf_tray'] == 'd') echo 'selected'; ?> >D</option>
                                </select>
                            </td>
                            <td class="odf_ports_td" align="center">
                                <span class="hidden_info"><?php echo $services['con_details_nttn_odf_port'] ?></span>
                                1<input type="checkbox" name="con_details_nttn_odf_port" value="1" />
                                2<input type="checkbox" name="con_details_nttn_odf_port" value="2" />
                                3<input type="checkbox" name="con_details_nttn_odf_port" value="3" />
                                4<input type="checkbox" name="con_details_nttn_odf_port" value="4" />
                                5<input type="checkbox" name="con_details_nttn_odf_port" value="5" />
                                6<input type="checkbox" name="con_details_nttn_odf_port" value="6" />
                                7<input type="checkbox" name="con_details_nttn_odf_port" value="7" />
                                8<input type="checkbox" name="con_details_nttn_odf_port" value="8" />
                                9<input type="checkbox" name="con_details_nttn_odf_port" value="9" />
                                10<input type="checkbox" name="con_details_nttn_odf_port" value="10" />
                            </td>
                            <td>
                                <select name="con_details_nttn_odf_circuit_type">
                                    <option value="">Please Select</option>
                                    <option value="primary" <?php if ($services['con_details_nttn_odf_circuit_type'] == 'primary') echo 'selected'; ?> >Primary</option>
                                    <option value="secondary" <?php if ($services['con_details_nttn_odf_circuit_type'] == 'secondary') echo 'selected'; ?> >Secondary</option>
                                </select>
                            </td>
                        </tr>
                    </table>
                </div>

我之后添加了一个必需的javascript文件,在那里,我声明了一个对象:

var odf_data = {
    num_ports_in_each_tray: 10,
    added_odfs_total: 0,
    must_update: false,
    uniq_ports_per_tray_collection: build_uniq_ports_collection()
};

function build_uniq_ports_collection() {
    var odf_ports = new Array(4);
    odf_ports['a'] = new Array(odf_data.num_ports_in_each_tray);
    odf_ports['b'] = new Array(odf_data.num_ports_in_each_tray);
    odf_ports['c'] = new Array(odf_data.num_ports_in_each_tray);
    odf_ports['b'] = new Array(odf_data.num_ports_in_each_tray);
    odf_names_array = new Array(); //odf names should be unique

    var error = '';
    $('div.odf_details').each(function() {
        //now we are inside each odf's immediate parent div
        var tray = $(this).find('td.odf_tray_td select.odf_tray :selected').val();
        var odf_name = $(this).find('td.odf_name_td input.odf_name').val();

        $(this).find('td.odf_ports_td input:checked').each(function() {
            var port = $(this).val();
            if (!error && odf_ports[tray][port] === undefined) {
                odf_ports[tray][port] = odf_name;
            } else {
                $(this).prop('checked', false);
            }
        });
        if (odf_data.must_update) {
            alert('there were some errors in port, you must update and save');
        }
    });
    return odf_ports;
}

但它在firebug控制台中显示错误:

odf_ports['a'] = new Array(odf_data.num_ports_in_each_tray);

TypeError: odf_data is undefined

我只是想,为什么? 我定义了对象吗?但那么为什么它被报告为未定义? 非常感谢任何快速帮助

1 个答案:

答案 0 :(得分:0)

喔, 感谢大家。 我得到了解决方案。

它基本上是在定义完成之前由属性调用的对象