One Pager Cheat Sheet
- We can create
objects
with certainattributes
that represent particularinstances
using aclass
, which will be explained in this lesson for JavaScript and TypeScript. - Classes provide structure to create objects that store data and define how it is used.
- The
Book
class stores important attributes such asTitle
,Publication Year
,ISBN
,Author Name
andNumber of Pages
in order to create differentBook
objects with various values. - The constructor of a
class
defines the parameters required to create a new object instance and sets values for the attributes of that object. - Calling the
paperbackOrHardcover()
method of a Book object will determine whether the book is ahardcover
orpaperback
. - The output of the
console.log()
statement isLife of Pi
, which is the value of thetitle
attribute ofmyBook
, initialized using its constructor and subsequently set equal to that ofmyBook2
. - We can use the
class
andnew
keywords in JavaScript to define objects and create instances, respectively, where the methods can refer to and set values of properties, whileget
andset
keywords can clarify the relationship between methods and properties. - To successfully create the object, the
new
keyword needs to be used when creating it, and then thename
property can be printed to the console. - In TypeScript, classes and objects use the same syntax as JavaScript and require that the properties of objects and their types be declared in the class definition and further services like parameter properties and arrow functions can be used to simplify the code.
- All properties in a TypeScript class must be initialized either in the class or the constructor.
- TypeScript adds some
features
such as Access Modifiers and Accessors to the traditionalJavaScript
structure of Classes and Objects. - The code is written in Typescript due to its syntax and use of the
constructor
and:
notation to declare properties. - Classes and objects allow developers to easily break down a real-world problem into manageable and modular components, making it easier to manage data.