- 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?
For purposes such as keeping a journal or storing key pairs in private/public key cryptography, it is useful to have data structures that can store multiple values as value-key pairs so that the relationship between variables becomes a part of the data itself. This is difficult / impossible to accomplish with using only strings / integers.
- What variable type can be used in order to solve the problem of storing multiple values?
Objects
- What are properties in Javascript?
Properties are expression that contain certain information about a value. E.g. the length .length property contains the length of an array of a string.
- Which values do not have properties?
null, undefined
- How can we access properties in a value (two ways)?
Properties can be accessed with either the dot or square bracket notation - string.length or string[length]. The difference between these notations is that the dot notation takes the expression after the dot as a string literal and tries to access a property with that name. The square bracket notation tries to evaluate the expression inside the square brackets, convert the result to a string and then access a property with that name. Dot notation only works with valid binding names, so no integers, spaces, etc.
- What are methods?
Properties that contain functions.
- What are objects?
They are an arbitrary collection of properties.
- 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)?
Storing key-value pairs, triplets, etc. in single variable and being able to manipulate the contents of that variable freely by assigning new properties, removing old ones, etc.
- How do you define an object?
We wrap an object in curly braces and use : inside the object to tie together key-value pairs. We separate key value pairs by commas. For example:
let person = {
name : “John”,
age : 37,
profession : “plumber”
}
In this example, name, age and profession are properties of the object person. For example, person.name returns “John”.
- What can you say about the mutability of Javascript objects?
They are mutable in the sense that their contents, i.e. properties can change, while their object value, i.e. identity stays the same. On the other hand, the contents of other variable types cannot be changed, e.g the code below logs “cat” because strings are immutable.
var test = “cat”;
test[0] = “r”
console.log(test);
- Why can’t you add new properties to a string variable?
Strings are not objects, so any paramaters you assign to them are not stored.
- What are rest parameters?
Rest parameters allow us call a function with an indefinite number of arguments, which are stored in an array represented by the rest parameter. This is useful if we don’t know how many parameters a function will have. A rest parameter is expressed by a three-dot notation before the last parameter. For example, let’s say we want a function that takes n numbers as input and multiplies these numbers, so we can write:
function multiplyN(…numbers){
var product = numbers[0];
numbers.shift();
for (number of numbers){
product *= number;
}
return product;
}
multiplyN(2, 2, 2); returns 8.
- What is serialisation and what is a use case of serialisation of data?
Serialization, as far as I understood from the book refers to extracting data from addresses in memory pointed to by variables and converting it to “flat descriptions” i.e. a particular format of said data for purposes of storage and communication.
- What is JSON?
It stands for Javascript Object Notation and it’s a popular serialization format.
- What are the differences between JSON and the way programmers write objects in plain Javascript?
JSON looks similar JS notation of arrays and objects, but all property names have to be surrounded by double quotes and no functions, bindings, comments or anything that requires computation is allowed.
A JSON entry might look like:
{
“name”:“Nara”
“present”:true
“hobbies”:[“fishing”, “hiking”]
}