TypeScript and Objects
One great thing about JavaScript is its object literal syntax. While in some languages, to create an object we have to do a lot of groundwork, such as creating classes and defining constructors, in JavaScript, and by extension in TypeScript, we can just create the object as a literal. So, if we want to create a person
object, with firstName
and lastName
properties, we only need to write the following:
const person = { firstName: "Ada", lastName: "Lovelace" }
JavaScript makes it easy to create and use the object, just like any other value. We can access its properties, pass it as an argument into methods
, receive it as a return
value from functions, and so on. And because of JavaScript's dynamic nature, it's very easy to add properties to our object. If we wanted to add an age
property to our object, we could just write the following:
person.age = 36;
However, because of...