Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 30th, 2012  |  syntax: None  |  size: 0.93 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Casting an object to a custom class
  2. function Card(newId, newName, newDescription)
  3. {
  4.     this.id = newId;
  5.     this.name = newName;
  6.     this.description = newDescription;
  7. }
  8.  
  9. function HashTable()
  10. {
  11.     var hashTableItems = {};
  12.  
  13.     this.SetItem = function(key, value)
  14.     {
  15.         hashTableItems[key] = value;
  16.     }
  17.  
  18.     this.GetItem = function(key)
  19.     {
  20.         return hashTableItems[key];
  21.     }
  22. }
  23.        
  24. ...
  25. var card = new Card(id, name, description);
  26.  
  27. $.viacognitaspace.cards.SetItem(id, card);
  28. ...
  29.        
  30. var cardObject = $.viacognitaspace.cards.GetItem(cardNumber);
  31.        
  32. $('#cardName').text(cardObject.name);
  33.        
  34. $.viacognitaspace = {} /* define namespace if not already defined... */
  35.  
  36. var card = new Card(id, name, description);
  37.  
  38. /* the "new" keyword creats an instance of your HashTable function
  39.    which allows you to reference and modify it later on... */
  40. $.viacognitaspace.cards = new HashTable();
  41.  
  42. $.viacognitaspace.cards.SetItem(id, card);