1

我写了一个字典对象或地图对象,无论您喜欢哪个名称,我都遇到了一个问题,在创建第二个对象后不会丢失对该对象的引用。
例如,我希望持有一个带有书籍和摘要的对象,以及一个带有汽车及其成本的对象。结果以两个映射都设置为第二个映射值结束。

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]);
}
4

1 回答 1

1

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

this.put = function()

this.firstKey = function()

this.lastKey = function()

...

使用this关键字,您正在为您的Dictionary[sort of] 类分配方法。您只是将功能公开。如果您查看诸如此类的函数,removeItemArray()则这些函数是私有的,并且只能从对象内部访问。

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

原型是用于构造所有新实例的基础,并且还将动态修改所有已构造的对象,因为在 Javascript 中对象保留指向原型的指针

原型的目的是提供在运行时向对象添加方法的能力。如果您来自传统的 OOP 语言,这可能看起来有点陌生,在传统的 OOP 语言中,您传统上预先定义了一个类及其方法。

看看那个问题中的其他答案,它们很好地解释了原型。

于 2012-11-29T07:35:19.923 回答