2012-11-29 106 views
1

我編寫了一個字典對象或地圖對象,您可以任意選擇您喜歡的名稱,並且在創建第二個對象後,我遇到了一個不會丟失對象引用的問題時間。例如,我希望拿着一本書和一個摘要,一本載有汽車及其成本的東西。結果以兩個映射都設置爲第二個映射值結束。如何防止Javascript對象丟失數組/對象參考

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

這是因爲您正在使用方法上的Dictionary.prototype。與this進行更換,也將努力:

this.put = function() 

this.firstKey = function() 

this.lastKey = function() 

... 

使用this關鍵字,你指定的方法來你的Dictionary [排序]類。您只是將這些功能公開化。如果你看看你的功能,如removeItemArray(),那麼這些功能是私人的,只能從對象內訪問。

原型的工作方式不同。這是回答一個不錯的報價,以this question約原型:

的protoype是將用於 構建所有新實例,也是基地,將修改dinamically都已經 構造的對象,因爲在Javascript的對象保留一個指向 原型的指針

原型的目的是爲了在運行時爲對象添加方法。如果您來自傳統上預先定義類和它的方法的傳統OOP語言,這可能看起來有點像外星人的概念。

看看這個問題中的其他答案,它很好地解釋了原型。

+0

它已經工作了,但爲什麼我對您使用此應用程序的時間以及您應該使用Prototype的時間感到困惑,您介意給出一個簡短的解釋嗎? – CubanAzcuy

+0

我有,而不是特別的鏈接,但我誤解了迴應,並認爲它作爲一個回落的情況下,它被稱爲一個未定義的情況有點像接口,向後延伸到一個object.I只是困惑如何動態發生了。謝謝!! – CubanAzcuy