Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This is a working file where I go
- //through the examples in the textbook
- //and will not be a complete representation
- //of the chapter's work
- //this is an example of an array
- //note the square brackets used
- let listOfNumbers = [2, 3, 5, 7, 11];
- console.log(listOfNumbers[2]);
- //this will return "5"
- //note the use of index values, base zero
- //Properties:
- //value.x calls a property: x
- //value[x] evaluates the expression in the brackets
- //in order to convert the result to a string as a property name
- //If you know the property you want, use value.x
- //If you want the property bound to a value, "x", use value[x]
- //Methods:
- //Properties that contain functions are generally called "methods"
- //eg: toUpperCase is a property of every string, and when called
- //will return a copy of the targeted string with every letter
- //converted to uppercase letters
- let sequence = [1, 2, 3];
- sequence.push(4);
- sequence.push(5);
- console.log(sequence);
- // -> [1, 2, 3, 4, 5]
- console.log(sequence.pop());
- // -> 5
- console.log(sequence);
- // -> [1, 2, 3, 4]
- //push is a method that adds values to the end of an array
- //pop is a method that removes the last value in an array and
- //returns what is left
- //Objects:
- //In order to represent the daily log entries as an array
- //we need to put each activity in an array and set up a boolean value
- //that will track whether or not Jacques turned into a squirrel
- //Values of the type "object" are arbitrary collections of properties
- //One way to create an object is to use braces as an expression
- let day1 = {
- squirrel:false,
- events: ["work", "touched tree", "pizza", "running"]
- };
- console.log(day1.squirrel);
- // -> false
- console.log(day1.wolf);
- // -> undefined (wolf was never made part of the expresssion of day1)
- day1.wolf = false;
- console.log(day1.wolf);
- // -> false
- // when an object is written over multiple lines, it can
- //be helpful for readability to indent after the last value that fits
- //on a line eg:
- let descriptions = [
- work: "Went to work",
- "touched tree": "Touched a tree"
- ];
- //reading a property that doesn't exist will give you the value "undefined"
- //you can assign a value to a property expression with the "=" operator
- //it will replace an existing property or create a new one if none exists
- //the "delete" operator will remove a named property from an object
- let anObject = {left: 1, right: 2};
- console.log(anObject.left);
- // -> 1
- delete anObject.left;
- console.log(anObject.left);
- // -> undefined
- console.log("left" in anObject);
- // -> false
- console.log("right" in anObject);
- // -> true
- // the binary "in" operator, when applied to a string and an object, tells
- //you whether the object has a property with that name or not
- //if a property is set to "undefined" the object still has it
- //but if the property is deleted the object no longer has the property at all
- //and an "in" operator will return "false"
- //the method "keys" will return an array with an object's property names
- //the method "assign" copies all properties of one object into another
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement