1. Read the sub-chapter called The weresquirrel. What problems does this chapter introduce that cannot be solved with variable types such as strings or integers?
Jacques is randomly turning into a weresquirrel, and needs to know what is causing his transformation from human -> squirrel. In order to figure out what triggers his condition, he has to collect lots of data to narrow down what the cause might be so he can avoid it and finally free himself from turning into a man-squirrel. This would present a problem if the only variable types were âstringâ or âintegerâ because heâs going to have a lot of data in his logâŚnot just 1 single data point that he can easily represent as a single string or integer.
For example, if Jacques wanted to store multiple values in the form of a string (lets say the values are the number of nuts he sees each day, recorded by day, for a week), his string would be awkwardly represented as â2 3 5 6 11 9 0â . However, this would not work well because every time you would want to access this data, you would have to convert the string back to a number to make it useable again.
2. What variable type can be used in order to solve the problem of storing multiple values?
Arrays can be used to solve the problem of storing multiple values because thatâs what they are designed to do! Be a list of values of the SAME TYPE that is easily indexable. Where the first element of the array is stored under the index of â0â NOT â1â as you might think.
an array is written like so:
let listOfNumber = [2, 3, 5, 7, 11];
console.log(listOfNumbers[2]);
//5
3. What are properties in Javascript?
A property is the association between a key (or name) and itâs value, and can be thought of as a key value pairing. It is constructed by joining a properties given name with itâs associated value. All values have an associated property minus the 2 mentioned in question 4âânullâ + âundefinedâ. In an array, the number in the index between the brackets is the name of the property being searched for within the array itâs applied to:
array[i] = returns element at index âiâ (also known as the property named âiâ).
4. Which values do not have properties?
âNullâ
âUndefinedâ
5. How can we access properties in a value (two ways)?
a) value.x - this accesses a property with the âproperty nameâ of âxâ. This grabs the property of âvalueâ with name âxâ. There is no evaluation, the â.xâ is the literal property name of the property being accessed.
b) value[x] - this accesses a property in a slightly different wayâŚby evaluating the expression within the â[]â and then takes the resultâconverted into a stringâas the âproperty nameâ
6. What are methods?
Properties that contain functions of the values they belong to. A good example of this is below where ââtoUpperCaseâ is a method of a stringâ:
let jake = âjakeâ;
console.log(jake.toUpperCase);
//JAKE
This type of property is special because instead of just returning a unique part of the value the method was called upon, but that already exists within the value it is applied to, a method applies a function to that value to manipulate the value before returning it. The example above shows this with the value being manipulated into all CAPS before being returned.
7. What are objects?
Objects are arbitrary collections of properties. They can be used to bundle multiple values together behind a single variable that houses everything. One way to create an object is highlighted in the following example that uses curly braces â{}â to house the contents of the object named âday1â which refers to day1 of Jacques log of squirrel transformations. The object shows that he did NOT turn into a squirrel that day despite having engaged in the array of events listed below. The property âsquirrelâ called on the value âday1â in the form âday1.squirrelâ returns âfalseâ because he never turned squirrel on this day:
let day1 = {
squirrel: false,
events: [âworkâ, âtouched treeâ, âpizzaâ, ârunningâ]
};
If you look at the example above, youâll notice that inside the curly braces â{}â lives a list of propertiesâin this case only 2âthat are separated using the â,â also known as the comma. These properties are broken up into 2 parts that are separated by a colon:
a) the property name - (in the example these were: squirrel, events)
b) the property value - (in the example these were the boolean of âfalseâ, and array starting w/ [âworkâ, âŚ]
The name is always to the left of the colon, and the value is always to the right. Now that objects have been introduced, we have seen all use cases for â{}â in javascript. When set equal to a variable, the braces represent an object. However, when at the start of a statement, they begin a block of statements (think loops+if statements).
delete removes a named property from an objectâŚhowever its not common practice. Example below:
let anObject = {left: 1, right: 2};
delete anObject.left;
console.log(anObject.left);
// â undefined
In operator tells whether or not an object has a specified property or not. If a property is deleted, then it will return false here, however if a property is set to âundefinedâ it will still return âtrueâ if the in function searches for it. Shown below in the example that follows from the previous example:
console.log(âleftâ in anObject);
// â false
console.log(ârightâ in anObject);
// â true
Object.keys is useful when determining what properties an object has. Given âobjectâ, object.keys will return an array of strings consisting of the objectâs property names.
8. What problem do objects solve that cannot be solved with other value types weâve learned so far (such as integer, string, array, boolean etc)?
Objects allow us to store multiple value types in one single variable. Without this data structure, we could not house multiple types of data together all under 1 roof. You could store a boolean, array, and string all in a single object. This is useful if you want to group data of a variety of types together based on the dataâs shared connection to the larger idea. In the weresquirrel story, this could be used to store all the data in Jacques log for day ONE (including: turnedSquirrel (boolean), locations visited (array), the daysâ outfit (string), etc.). Using an object is the only way Javascript can complete this task.
9. How do you define an object?
An object is a datastructure that can house any other type of datastructure and is a sum of itâs subsidiary properties (a key/value pair) that compose the body of the object. These properties can be any data type you need them to be.
10. What can you say about the mutability of Javascript objects?
Objects in Javascript are very mutable because many functions (such as âdeleteâ) can change the properties that compose the greater object. In this way, objects are different than the original types of values discussed (numbers, strings, and Booleans), which are all **IMMUTABLE*âmeaning that it is impossible to change the value of these types after the value has been created. Put more simply, once a string is createâas in the word âcatâ, it is impossible for another piece of code to change the first letter of the wordâas in âcatâ being changed to âratâ. No matter what, the value of that string will remain the same to JavaScript.
Objects work differently because at 2 different times you could have the same objectâafter changing some of itâs propertiesâyield 2 different versions of itselfâŚsomething unheard of with the previously mentioned Immutable data types. A way to visualize this is through the following example that showcases how an object containing the same properties is not the same as the objects being binded to one anotherâŚthis is due to the mutable nature of Objects. If an object starts out being built from identical properties as another object, and then 1 of them experiences a change in their properties (donât forget, objects are mutable so this 100% possible), then the 2 are no longer the ~same~ object as one anotherâin reality they never were to begin with, but this is the rational for why thatâs the case. The code example that follows will help illustrate this:
let object1 = {value: 10};
let object2 = object1;
let object3 = {value: 10};
console.log(object1 == object2);
// â true
console.log(object1 == object3);
// â false
object1.value = 15;
console.log(object2.value);
// â 15
console.log(object3.value);
// â 10
One final thing to note about mutability is with regards to bindings. Despite what may be true for the mutability of different value types, bindings are still mutable because you can change the value the binding points at (regardless of whether or not you can change the actual value itself). For example:
const score = {visitors: 0, home: 0};
// This is okay
score.visitors = 1;
// This isnât allowed
score = {visitors: 1, home: 1};
SECOND PART:
1. Why canât you add new properties to a string variable?
The âstringâ data type is immutable and does NOT save the properties that you add to the string. This is because the âstringâ data type (like ânumberâ and âbooleanâ) is not an object.
2. What are rest parameters?
A ârest parameterâ is a parameter for a function that can accept any number (aka an infinite amount of) arguments when passed to the function. A good example of this is the Math.max function that computes the max of ALL arguments given to it (and there is no limit to how many arguments it can accept). This is written by leaving 3 dots before the parameters name in the function construction as shown below:
function max(âŚnumbers) {
let results = -Infinity;
for (let number of numbers) {
if (number > result) {
result = number;
}
}
return result;
}
console.log(max(4, 1, 9, -2));
// -> 9
The values of a REST PARAMETER are bundled into an array with all others associated with that particular argument. If thereâs other parameters to meet before the rest parameter, then they will NOT be packaged in the array too, but instead stored as their own separate entity.
4. What is serialisation and what is a use case of serialisation of data?
The process of turning data into a format more suitable for transfer to another computer. This creates a flat description of the data rather than packaging and exporting the entirety of a computers memory to another user. The best known use case for this data manipulation technique is turning object properties (ie. their names + associated values) into JSON format so it is easily readable by otherâs machines.
5. What is JSON?
A serialized format used commonly for data transfer on the web in JavaScript (and even in other languages). It stands for JavaScript Object Notation and looks very similar to JavaScriptâs way of writing arrays and objects with a few restrictions:
a) property names must be surrounded by double quotes
b) only simple data expressions can be included <- (ie. nothing that requires computation like function calls or bindings)
c) no comments allowed
Example of format as journal entry:
{
âsquirrelâ: false,
âeventsâ: [âworkâ, âtouched treeâ, âpizzaâ, ârunningâ]
}
6. What are the differences between JSON and the way programmers write objects in plain Javascript?
mentioned above, here they are again:
a) property names must be surrounded by double quotes
b) only simple data expressions can be included <- (ie. nothing that requires computation like function calls or bindings)
c) no comments allowed