如何防止Javascript对象丢失数组/对象参考

时间:2012-11-29 07:29:42

标签: javascript javascript-objects

我写了一个字典对象或地图对象,这个对象就是你喜欢的名字,而且在创建同一个第二个对象后,我遇到的问题是没有丢失对象的引用。
例如,我希望持有一个带有书籍和摘要的对象,一个带有汽车及其成本的对象。结果以两个map都设置为第二个map值结束。

function Dictionary() {
    var _size = 0;
    var _dict = new Object();
    var _keys = new Array();
    var _values = new Array();
    var _firstKey = "";
    var _lastKey = "";

    Dictionary.prototype.put = function () {
        if (_size == 0)
            _firstKey = arguments[0];

        _keys.push(arguments[0]);
        _values.push(arguments[1]);
        _dict[arguments[0]] = arguments[1];
        _lastKey = arguments[0];
        _size++;
    };

    Dictionary.prototype.firstKey = function () {
        return _firstKey;
    };

    Dictionary.prototype.lastKey = function () {
        return _lastKey;
    };

    Dictionary.prototype.get = function () {
        return _dict[arguments[0]];
    };

    Dictionary.prototype.size = function () {
        return _size;
    };

    Dictionary.prototype.entrySet = function () {
        return _dict;
    };

    Dictionary.prototype.key = function () {
        return _keys;
    };

    Dictionary.prototype.vaules = function () {
        return _values;
    };

    Dictionary.prototype.clear = function () {
        _size = 0;
        _dict = new Object();
        _keys = new Array();
        _values = new Array();
        _firstKey = "";
        _lastKey = "";
    };

    Dictionary.prototype.remove = function () {
        var keyIndex;
        if (_size >= 1) {
            for (var i = 0; i < _keys.length; i++) {
                if (arguments[0] == _keys[i])
                    keyIndex = i;
            }

            if (keyIndex === undefined)
                return undefined

            _dict = removeItemObject(_dict, arguments[0]);
            _keys = removeItemArray(_keys, keyIndex);
            _values = removeItemArray(_values, keyIndex);

            if (_keys.length > 0 && _keys.length == keyIndex) {
                _lastKey = _keys[keyIndex - 1];
            }

            if (_keys.length == 0)
                _lastKey = undefined;

            if (0 == keyIndex) {
                if (_keys.length > 1)
                    _firstKey = _keys[1];

                else if (_keys.length > 0)
                    _firstKey = _keys[0];

                else if (_keys.length == 0)
                    _firstKey = undefined;
            }

            _size--;

        }
    };

    Dictionary.prototype.serialize = function () {
        var serializedFJSON = "{";
        serializedFJSON += "\"size\"" + ":" + _size + ",";
        serializedFJSON += "\"dict\"" + ":" + JSON.stringify(_dict) + ",";
        serializedFJSON += "\"keys\"" + ":" + JSON.stringify(_keys) + ",";
        serializedFJSON += "\"values\"" + ":" + JSON.stringify(_values) + ",";
        serializedFJSON += "\"firstKey\"" + ":" + "\"" + _firstKey + "\"" + ",";
        serializedFJSON += "\"lastKey\"" + ":" + "\"" + _lastKey + "\"" + "";
        serializedFJSON += "}";
        return serializedFJSON;
    };

    Dictionary.prototype.deserialize = function () {
        var DictionaryClone = JSON.parse(arguments[0]);
        _size = DictionaryClone.size;
        _dict = DictionaryClone.dict;
        _keys = DictionaryClone.keys;
        _values = DictionaryClone.values;
        _firstKey = DictionaryClone.firstKey;
        _lastKey = DictionaryClone.lastKey;
    };

    function removeItemArray(arrayName, key) {
        var x;
        var tmpArray = new Array();
        for (x in arrayName) {
            if (x != key) { tmpArray[x] = arrayName[x]; }
        }
        return tmpArray;
    };

    function removeItemObject(arrayName, key) {
        var x;
        var tmpArray = new Object();
        for (x in arrayName) {
            if (x != key) { tmpArray[x] = arrayName[x]; }
        }
        return tmpArray;
    };
}

var m = new Dictionary();
m.put("Lord Of The Rings", "Not One Book But, Three");
m.put("Curious George", "I Don't Know Something About a Guy In A Rain Coat");

var k = new Dictionary();
k.put("Scion FRS", "24955");
k.put("Toyota Camry", "22055");

k.remove("Toyota Camry");

for (items in m.entrySet()) {
    alert(items + " " + m.entrySet()[items]);
}

1 个答案:

答案 0 :(得分:1)

这是因为您在方法上使用Dictionary.prototype。将它们替换为this,它将起作用:

this.put = function()

this.firstKey = function()

this.lastKey = function()

...

使用this关键字,您要为Dictionary [种类]类分配方法。你只是公开这些功能。如果您查看removeItemArray()等函数,则这些函数是私有的,只能从对象中访问。

原型的工作方式不同。这是关于this question关于原型的答案的一个很好的引用:

  

protoype是将用于的基础   构建所有新实例,并且还将修改所有新实例   构造对象,因为在Javascript对象中保留了一个指针   原型

原型的目的是赋予在运行时向对象添加方法的能力。如果您来自传统的OOP语言,传统上预先定义一个类及其方法,这似乎有点像外星人的概念。

查看该问题中的其他答案,这些答案可以很好地解释原型。