JavaScript I know-Data Structure-Hashtable

Source: Internet
Author: User

 

1 function Hashtable (){

2 this. _ hashValue = new Object ();

3 this. _ iCount = 0;

4}

5 Hashtable. prototype. add = function (strKey, value ){

6 if (typeof (strKey) = "string "){

7 this. _ hashValue [strKey] = typeof (value )! = "Undefined "? Value: null;

8 this. _ iCount ++;

9 returntrue;

10}

11 else

12 throw "hash key not allow null! ";

13}

14 Hashtable. prototype. get = function (key ){

15 if (typeof (key) = "string" & this. _ hashValue [key]! = Typeof ('undefined ')){

16 returnthis. _ hashValue [key];

17}

18 if (typeof (key) = "number ")

19 returnthis. _ getCellByIndex (key );

20 else

21 throw "hash value not allow null! ";

22

23 returnnull;

24}

25 Hashtable. prototype. contain = function (key ){

26 returnthis. get (key )! = Null;

27}

28 Hashtable. prototype. findKey = function (iIndex ){

29 if (typeof (iIndex) = "number ")

30 returnthis. _ getCellByIndex (iIndex, false );

31 else

32 throw "find key parameter must be a number! ";

33}

34 Hashtable. prototype. count = function (){

35 returnthis. _ iCount;

36} www.2cto.com

37 Hashtable. prototype. _ getCellByIndex = function (iIndex, bIsGetValue ){

38 vari = 0;

39 if (bIsGetValue = null) bIsGetValue = true;

40 for (var key in this. _ hashValue ){

41 if (I = iIndex ){

42 returnbIsGetValue? This. _ hashValue [key]: key;

43}

44 I ++;

45}

46 returnnull;

47}

48 Hashtable. prototype. remove = function (key ){

49 for (var strKey in this. _ hashValue ){

50 if (key = strKey ){

51 deletethis. _ hashValue [key];

52 this. _ iCount --;

53}

54}

55}

56 Hashtable. prototype. clear = function (){

57 for (var key in this. _ hashValue ){

58 delete this. _ hashValue [key];

59}

60 this. _ iCount = 0;

61}

Explanation: Hashtable is one of the most common data structures in c #, but there are no data structure objects in JavaScript. However, we can use some features of Dynamic Language to implement some common data structures and operations, which can make some complex code logic clearer, it is also more in line with the packaging principles advocated by object-oriented programming. Here, we can use the JavaScriptObject Object to dynamically add attributes to implement Hashtable. here we need to note that JavaScript can traverse all attributes of the Object through the for statement. However, this method should be avoided unless you really know what is put in your object.

[By the way]: StringCollection/ArrayList/Stack/Queue can be used to expand JavaScript.

From the rain of November

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.