Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. //This is a working file where I go
  2. //through the examples in the textbook
  3. //and will not be a complete representation
  4. //of the chapter's work
  5.  
  6.  
  7. //this is an example of an array
  8. //note the square brackets used
  9. let listOfNumbers = [2, 3, 5, 7, 11];
  10. console.log(listOfNumbers[2]);
  11. //this will return "5"
  12. //note the use of index values, base zero
  13.  
  14. //Properties:
  15. //value.x calls a property: x
  16. //value[x] evaluates the expression in the brackets
  17. //in order to convert the result to a string as a property name
  18. //If you know the property you want, use value.x
  19. //If you want the property bound to a value, "x", use value[x]
  20.  
  21. //Methods:
  22. //Properties that contain functions are generally called "methods"
  23. //eg: toUpperCase is a property of every string, and when called
  24. //will return a copy of the targeted string with every letter
  25. //converted to uppercase letters
  26. let sequence = [1, 2, 3];
  27. sequence.push(4);
  28. sequence.push(5);
  29. console.log(sequence);
  30. // -> [1, 2, 3, 4, 5]
  31. console.log(sequence.pop());
  32. // -> 5
  33. console.log(sequence);
  34. // -> [1, 2, 3, 4]
  35. //push is a method that adds values to the end of an array
  36. //pop is a method that removes the last value in an array and
  37. //returns what is left
  38.  
  39. //Objects:
  40. //In order to represent the daily log entries as an array
  41. //we need to put each activity in an array and set up a boolean value
  42. //that will track whether or not Jacques turned into a squirrel
  43. //Values of the type "object" are arbitrary collections of properties
  44. //One way to create an object is to use braces as an expression
  45. let day1 = {
  46. squirrel:false,
  47. events: ["work", "touched tree", "pizza", "running"]
  48. };
  49. console.log(day1.squirrel);
  50. // -> false
  51. console.log(day1.wolf);
  52. // -> undefined (wolf was never made part of the expresssion of day1)
  53. day1.wolf = false;
  54. console.log(day1.wolf);
  55. // -> false
  56. // when an object is written over multiple lines, it can
  57. //be helpful for readability to indent after the last value that fits
  58. //on a line eg:
  59. let descriptions = [
  60. work: "Went to work",
  61. "touched tree": "Touched a tree"
  62. ];
  63. //reading a property that doesn't exist will give you the value "undefined"
  64. //you can assign a value to a property expression with the "=" operator
  65. //it will replace an existing property or create a new one if none exists
  66. //the "delete" operator will remove a named property from an object
  67. let anObject = {left: 1, right: 2};
  68. console.log(anObject.left);
  69. // -> 1
  70. delete anObject.left;
  71. console.log(anObject.left);
  72. // -> undefined
  73. console.log("left" in anObject);
  74. // -> false
  75. console.log("right" in anObject);
  76. // -> true
  77. // the binary "in" operator, when applied to a string and an object, tells
  78. //you whether the object has a property with that name or not
  79. //if a property is set to "undefined" the object still has it
  80. //but if the property is deleted the object no longer has the property at all
  81. //and an "in" operator will return "false"
  82. //the method "keys" will return an array with an object's property names
  83. //the method "assign" copies all properties of one object into another
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement