1. var myObj = {
  2. name: "Frank",
  3.  
  4. };
  5.  
  6. console.log( myObj.hasOwnProperty('name') ); // should print true
  7. console.log( myObj.hasOwnProperty('nickname') ); // should print false
  8.  
  9. var myObj1 =
  10. {
  11.         "thing" = 1;
  12. };
  13.  
  14. var myObj2 =
  15. {
  16.         thing = 1;
  17. };
  18.  
  19. var myObj3 =
  20. {
  21.         "thing with a long name" = 1;
  22.         "other thing" = 2;
  23. };
  24.  
  25. function MyObj( x )
  26. {
  27.         this.thing = x;
  28. };
  29.  
  30. var myObj4 = new MyObj( 5 );
  31.  
  32. myObj1["thing"] // resolves to 1
  33. myObj1.thing // resolves to 1
  34. myObj2["thing"] // resolves to 1
  35. myObj2.thing // resolves to 1
  36. myObj3["thing with a very long name"] // resolves to 1
  37. myObj3["other thing"] // resolves to 2
  38.  
  39. var whichThing = "thing";
  40.  
  41. myObj1[whichThing] // resolves to 1
  42.  
  43. whichThing = "thing with a long name";
  44. myObj3[whichThing] // resolves to 1
  45.  
  46. whichthing = "thing";
  47. myObj4.thing // resolves to 5
  48. myObj4["thing"] // resolves to 5
  49. myObj4[whichThing] // resolves to 5

Posted by bodie at 18 Nov 2013, 05:49:57 Etc/UTC
Language: javascript