First part
- In the introduced wiresquirrel scenario, a protagonist named Jacques every now and then transforms into a squirrel. To find out what triggers this change, he decides to keep a daily log of everything he does on day and whether the transformation happened. For this task he needs a certain data structure to store this information; simple built-in types like booleans, numbers and strings are not sufficient (though it is theoretically possible to store large amounts of data in a single large string, it would be extremely complex to access/modify certain parts), he rather needs a way to represent sequences of values as well as a way of grouping different values into a single value.
- To solve the problem of storing multiple values we can use arrays and in a more general way the Object type in JavaScript (in fact, arrays are just special types of objects).
- Properties are special values attached to objects, accessible via a certain key. Almost all values in JavaScript contain properties, exceptâŚ
- âŚ
null
andundefined
. - Properties can be accessed either with a dot or with square brackets:
someObject.myProperty
vs.someObject["myProperty"]
In the first notation, the dot follows a literal name of the property, while on the bracket notation, the expression between the brackets is first evaluated to get the property name. - Methods are properties that hold function values.
- Objects are arbitrary collections of properites.
- Objects allow us to group several values into a single value (see also question 1).
- An object is defined with an expression of curly brackets (
{ ... }
) where inside the brackets is a comma-separated list of properties. Each property consists of a name, followed by a colon and a value. Example for an object describing a person:
let myself = { age : 30, sex : "male", nickname : "theStack", };
- In contast to simple built-in types as numbers, booleans and strings, objects are mutable, i.e. their properties can have different values at different times.
Second part coming soonâŚ