在JavaScript中分配对象属性会以奇怪的方式更改对象

时间:2015-12-14 14:33:08

标签: javascript

我正在调试基于Ext的复杂JS客户端框架。我偶然发现了一条线,它给出了我无法以任何方式解释的结果。这是一行(me实际上只是this的别名):

me.displayTplData = displayTplData;

执行前的一些值:

me.value: "87172981"
displayTplData: Array[1]  (this is a local variable)
me.displayTplData: undefined

在行之后(F11,"进入下一个函数调用"):

me.value: null
displayTplData: Array[1]  (stays as before)
me.displayTplData: null

不仅分配显然没有发生,这也改变了分配给不相关属性value的值......我能想到的唯一方法是displayTplData是否有相关的setter (类似于Python中的描述符?)。但另一方面,JS调试器在执行该行时不会进入任何代码。此外,这个框架适用于IE8 +,因此它肯定不会使用任何最近的JS开发。

FireFox和Chrome都会发生这种情况,因此它必须是一些"这应该以这种方式工作",但我完全不了解发生了什么。

有人可以猜出可能的原因吗?很抱歉,我无法将其简化为一个独立的示例。

修改

这是完整的功能,作为上下文。

setValue: function(value, doSelect) {
    var me = this,
        valueNotFoundText = me.valueNotFoundText,
        inputEl = me.inputEl,
        i, len, record,
        dataObj,
        matchedRecords = [],
        displayTplData = [],
        processedValue = [];

    if (me.store.loading) {
        // Called while the Store is loading. Ensure it is processed by the onLoad method.
        me.value = value;
        me.setHiddenValue(me.value);
        return me;
    }

    // This method processes multi-values, so ensure value is an array.
    value = Ext.Array.from(value);

    // Loop through values, matching each from the Store, and collecting matched records
    for (i = 0, len = value.length; i < len; i++) {
        record = value[i];
        if (!record || !record.isModel) {
            record = me.findRecordByValue(record);
        }
        // record found, select it.
        if (record) {
            matchedRecords.push(record);
            displayTplData.push(record.data);
            processedValue.push(record.get(me.valueField));
        }
        // record was not found, this could happen because
        // store is not loaded or they set a value not in the store
        else {
            // If we are allowing insertion of values not represented in the Store, then push the value and
            // create a fake record data object to push as a display value for use by the displayTpl
            if (!me.forceSelection) {
                processedValue.push(value[i]);
                dataObj = {};
                dataObj[me.displayField] = value[i];
                displayTplData.push(dataObj);
                // TODO: Add config to create new records on selection of a value that has no match in the Store
            }
            // Else, if valueNotFoundText is defined, display it, otherwise display nothing for this value
            else if (Ext.isDefined(valueNotFoundText)) {
                displayTplData.push(valueNotFoundText);
            }
        }
    }

    // Set the value of this field. If we are multiselecting, then that is an array.
    me.setHiddenValue(processedValue);
    me.value = me.multiSelect ? processedValue : processedValue[0];
    if (!Ext.isDefined(me.value)) {
        me.value = null;
    }
    me.displayTplData = displayTplData; //store for getDisplayValue method   <------- this is the line
    me.lastSelection = me.valueModels = matchedRecords;

    if (inputEl && me.emptyText && !Ext.isEmpty(value)) {
        inputEl.removeCls(me.emptyCls);
    }

    // Calculate raw value from the collection of Model data
    me.setRawValue(me.getDisplayValue());
    me.checkChange();

    if (doSelect !== false) {
        me.syncSelection();
    }
    me.applyEmptyText();

    return me;
},

1 个答案:

答案 0 :(得分:0)

有时调试器会提供错误信息。奇怪的是,Firefox和Chrome的调试器都会产生相同(错误)的检查,但是如果你想确定这些值,只需在语句之前和之后放置console.log(me.value),看看会打印什么。