Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. /*
  2. * Objects: Key / Value Pairs
  3. *
  4. * Collection
  5. */
  6.  
  7. // {} is an empty literal object
  8. console.log(typeof {}); // -> 'object'
  9.  
  10. var exampleObj = {
  11. aKey: 'a value',
  12. anotherKey: 'another value!'
  13. };
  14.  
  15. // accessing values through DOT syntax
  16. console.log(exampleObj.aKey); // -> 'a value'
  17. console.log(exampleObj.anotherKey); // -> 'another value!'
  18.  
  19.  
  20. // objects are associative arrays
  21. // the key is associated with the value
  22. var user = {
  23. firstName: 'Jane',
  24. lastName: 'Doe'
  25. };
  26.  
  27.  
  28. // keys are strings...
  29. // accessing values through ARRAY notation/BRACKET notation
  30. console.log(exampleObj['aKey']); // -> 'a value'
  31. console.log(exampleObj['anotherKey']); // -> 'another value!'
  32.  
  33.  
  34. // adding keys
  35. user.age = 22;
  36. user.petName = 'Jax';
  37. console.log(user); // -> {firstName: 'Jane', lastName: 'Doe', age: 22, petName: 'Jax'}
  38.  
  39. // deleting keys
  40. delete user.petName;
  41. console.log(user); // -> {firstName: 'Jane', lastName: 'Doe', age: 22}
  42.  
  43. // get all the keys in an object
  44. var userKeys = Object.keys(user); // -> ['firstName', 'lastName', 'age']
  45.  
  46. // userKeys.length will give the 'length' of an object
  47.  
  48.  
  49. // objects can hold anything
  50. var anything = {
  51. ayo: 'egad',
  52. pi: 3.14592,
  53. no: false,
  54. yeye: {},
  55. kk: function() { console.log('kk'); },
  56. lol: []
  57. };
  58.  
  59.  
  60. // looping over an object's keys
  61. for(var properyName in user) {
  62. console.log('key: ' + properyName, 'value in key: ' + user[properyName]);
  63. console.log("\n");
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement