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?
2. What variable type can be used in order to solve the problem of storing multiple values?
Arrays, because they can store a series of values, and those values can even be of different data types.
3. What are properties in Javascript?
from w3schools:
-"Properties are the values associated with a JavaScript object
- A JavaScript object is a collection of unordered properties
- Properties can usually be changed, added, and deleted, but some are read only.
- Property names are strings.
4. Which values do not have properties?
-null and undefined do not have properties.
5. How can we access properties in a value (two ways)?
- There are two ways to acces properties value.x and value[x]
- “When useing a dot, the word after the dot is the literal name of the property”
- "When using square brackets, the expression between the brackets is evaluated to get a property name.
- value.x fetches the property of value named ‘x’, value[x] tries to evaluate the expression x and uses the result, coverted to a string, as the property name.
- The dot notation can only be used if the property name has a valid binding name. If you wanted to access a property named 2 or John Doe, you must use square brackets:
value[2] or value[“John Doe”]
6. What are methods?
Properties that contain functions are generally called methods of the value they belong to, as in ‘toUpperCase is a method of a string’.
someArray.push(“someValue”) or someArray.pop() are methods for manipulating an array.
7. What are objects?
Objects are variables but can contain many values from a variety of datatypes. The values are an (unordered) list of pairs of property names and their corresponding values.
"JavaScript objects are containers for named values called properties or methods.
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)?
"A set of daily log entries can be represented as an array. But what we are looking for is an entry that stores a list of activities AND a Boolean value that indicates whether Jacques turned into a squirrel or not.
9. How do you define an object?
'Values of the type object are arbitrary collections of properties. One way to create an object is by using braces as an expression. Inside the braces, there is a list of properties separated by commas.
Each property has a name followed by a colon and a value. Properies whose names aren’t valid binding names or valie numbers have to be quoted.
let someObject = {
age: 35,
hairColor: “brown”
};
console.log(someObject.age);
//-> 35
10. What can you say about the mutability of Javascript objects?
Object values can be modified. The types of values discussed in earlier chapters, such as numbers, strings, and Booleans, are all immutable.
You can change object properties, causing a single object value to have different content at different times.
Using the const binding to an object can itself not be changed and will continue to point at the same object, the contents of that object might change.
const score = {visitors: 0, home: 0};
//This is ok
score.visitors = 1;
//This is not allowed
score = {visitors:1, home: 1};
Bonus
The elements in an array are stored as the array’s properties, using numbers as property names. Because you can’t use the dot notation with numbers and usually want to use a binding that holds the index anyways, you have to use the bracket notation to get at them.
The length property of an array tells us how many elements it has. This property name is a valid binding name, you typically write array.length because that’s easier to write than array[‘length’]
PART II
Strings and their properties
1. Why can’t you add new properties to a string variable?
"We can READ propeties like length and toUpperCase from string values. But if you try to add a new property, it doesn’t stick.
"Values of type string, number, and Boolean are immutable and cannot be changed. But these types do have built-in properties, and every
string value has a number of methods, such as slice, and indexOf, which resemble array methods of the same name.
2. What are rest parameters?
A rest parameter …lastArgument allows your functions to accept any (unknown) number of arguments it might be given.
To do this, you put three dots before the function’s last parameter
"When such a function is called, the rest parameter is bound to an array containing all further arguments. If there are other parameters before it, their values aren’t part of that array.
3. (Feel free to skip the sub-chapter on Math object and Destructuring)
4. What is serialization and what is a use case of serialization of data?
"If you want to save data in a file for later or send it to another computer over the network, you have to somehow convert these tablges of memory addresses to a description that can be stored or sent. **
You COULD send over your entire computer memory along with the address of the value you’re interested in, I supposed, but that doesn’t seem like the best approach.
What we can do is SERIALIZE the data. That means it is converted into a flat description. A popular format is JSON.
5. What is JSON?
JavaScript Object Notation is a widely used data storage and communication format on the web, even in languages other than JavaScript.
JSON looks similar to JavaScript’s way of writing arrays and objects, with a few restrictions.
6. What are the differences between JSON and the way programmers write objects in plain Javascript?
All property names have to be surrounded by DOUBLE QUOTES, and only simple data expressions are allowed- no function calls, binding, or anything that involves actual computation.
Comments are not allowed in JSON.
Note: JavaScript gives us the function JSON.stringify and JSON.parse to convert data to and from this format. The first takes a JavaScript value and returns a JSON-encoded string. The second takes such a string and converts it to the value it encodes.
let string = JSON.stringify({squirrel: false, events: [“weekend”]});
console.log(string)
//->{“squirrel”:false,“events”:[“weekend”]}
console.log(JSON.parse(string).events);
//->[“weekend”]