Objects
Objects are collections of properties and actions that can be used in code and have more complex behaviors. In JavaScript, almost everything is an object, but there are a few exceptions, like the primitive data types, so an object is considered a data type by itself.
Objects are declared as a collection of key-value pairs separated by
:and surrounded by the{}characters, calling this structure a block
let a = { // Declaration of an object
name: "Joe"
};
let b = {id: 1, name: "Sara"}; // Can also be declared in a single lineThere are also some special built-in objects and types. Here is an example:
[1, 2, 3] // Array, an ordered list-like collection with special behavior
function f() {} // Function, reusable sets of statements
2024-06-21T17:55:33.711Z // Date, for storing date and time value
Math..random() // Built-in Object with mathemathical functionsAttributes and Methods
These are the main characteristics that define objects in JavaScript.
Attributes are named properties associated with an object that store data or states related to that object. They can be understood as variables in the scope of an object. Their values are accessed with the name of the object and the Dot Operator. For example:
let user = {
name: "Joe",
age: 30
};
console.log(user.age); // Access the value of the attribute age, prints 30The Dot Operator also lets us modify and add attributes to an object. For removing a property from an object, we use the
deleteoperator. For example:
We can also use bracket notation instead of the Dot Operator to access the properties of an object. This lets us also pass variables as keys for accessing values. For example:
Methods are properties whose value is a function. They define actions or behaviors for the object that, when called, can operate on its attributes. They are also accessed with the name of the object, the Dot Operator, and the
()characters like functions. For example:
In the same way that attributes, the Dot Operator also lets us modify and add methods to an object. For example:
Objects can also be nested, and in the same way, we can use the Dot operator and the key-value notation multiple times, or even combine them. For example:
Objects are modified permanently even when they are passed to a function
We can iterate through the attributes of an object with loops
All JavaScript built-in objects have their own attributes and methods. For example:
Factory Functions and Destructured Assignment
Factory Functions are used to create objects easily and quickly. They receive as parameters all attributes of the object, set them, and return a new object to be used
When we want to extract key-value pairs from objects and save them as variables, we can use a notation called Destructured Assignment, which lets us do it quickly. This works to assign multiple properties, nestes properties, and event methods
The this Keyword
The this keyword is used to point to the attributes and methods that are defined in an object. This makes the execution flow to search for the variables/functions needed in the defined attributes and methods of the object, instead of the ones defined or passed from the outside.
We use the this keyword to specify that the variables are attributes from the object
If we donΒ΄t use the
thiskeyword, the attributes are not recognized in other parts of the object, and this will cause an error
The
thiskeyword does not work for arrow functions as they use their own scope and not the object scope. Trying to access an attribute from the object will just returnundefined
Getters and Setters
Getters are methods that get and return the internal properties of an object. They can be used to retrieve properties of an object safely, doing validations in the data stored
Setters are methods that reassign values of existing properties within an object. They can be used to modify properties of an object safely, doing validations in the data to be inserted
Last updated