Read the sub-chapter called The weresquirrel. What problems does this chapter introduce that cannot be solved with variable types such as string or integers?
- Variable types like strings and integers can only hold a single type of data, and data structure is needed that is capable of storing multiple values and datatypes to keep everything organized.
What variable type can be used in order to solve the problem of storing multiple values?
- You can use array data sets to solve the problem of storing multiple values.
What are properties in Javascript?
-
A few suspicious looking expressions like “myString.length” (to get the length of a string) and “Math.max” (the maximum function) are expressions that access a proper of some value.
-
In the first case, we access the “length” property of the value in “myString”, in the second, we access the proper named “max” in the “Math” object (which is a collection of mathematics-related constants and functions)
-
Almost all of JavaScript values have properties, except null and undefined.
-
The two main ways to access properties in JavaScript are with a dot and with square brackets .
-
E.g. value.x fetches the property of value named “x” — value[x] tries to evaluate the expression “x” and uses the result, converted to a string , as the property name.
-
The elements in an array are stored as the array’s properties, using numbers as property names.
-
The length property of an array tells us how many elements it has.
Which values do not have properties?
- Null & undefined.
How can we access properties in a value (two ways)?
- .x & [x].
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,
-
The push method adds values to the end of an array, and the pop method does the opposite, removing the last value in the array and returning it.
What are objects?
-
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.
-
When an object is written over multiple lines, indenting it helps with readability.
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 are able to hold as many different datatypes as needed.
How do you define an object?
-
Object are like variables that are containers for data values, but objects can contain many values.
-
The values are written as name:value pairs (name and value separated by a colon)
-
The name:values pair in JavaScript objects are called properties:
— Property
— Property Value
- The object class represents one of JavaScript’s data types, used to store various keyed collections and more complex entities.
What can you say about the mutability of JavaScript objects?
-
Objects work differently than value types like (numbers, strings, and booleans which are immutable).
-
You can change their properties, causing a single object value to have different content at different times.
Why can’t you add new properties to a string variable?
- Value of type string, number, and Boolean are not objects, and though the language doesn’t complain if you try to set new properties on them, it doesn’t actually store those properties, they are immutable.
What are rest parameters?
-
Rest parameters allow functions to accept any number of arguments.
-
To write such a function. 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.
What is the serialization and what is a use case of serialization of data?
-
Serialization means to convert an object into that string.
-
It’s use case is being able to save the data to a file or for transferring it to another computer on the network.
What is JSON?
-
The JSON object contains methods for parsing JavaScript Object Notation and converting values to JSON.
-
It can’t be called or constructed, and aside from its two method properties, it has no interesting functionality of its own.
-
JSON is a syntax for serializing objects, arrays, numbers, strings, booleans, and null.
-
JSON looks similar to JavaScript’s way of writing arrays and objects, with a few restrictions. All property names have to be surrounded by double quotes, and only simple data expressions are allowed — no function calls, bindings, or anything that involves actual computation. (Comments are not allowed in JSON)