Classes

In object-oriented programming (OOP), a class serves as a blueprint for creating objects. It defines the attributes (data) and methods (functions) that the objects instantiated from the class will have.

Drawio diagrams on classes

Image

This is a class diagram of adventureGame. This version is the one that contains my personal changes to the original. Diagrams like this model the relationships between different classes. The top section of a box contains the key properties it has. The bottom half of a box shows all the methods a class has. The arrows between boxes represent is a and has a relationships. The arrows with an open arrow head represent inheritence. The class the arrow points to is the class the other is inheriting from. When a class is extended from another class, it inherits all its properties and methods. These would also model is a relationships. For example, Character is a GameObject. The other arrows with a filled in arrow head are for has a relationships. They represent if a class has objects/instances of another class in it. For example, GameEnv has GameObjects it it.

Instantiating Objects

Instantiation is the process of creating an object from a class using a constructor method. The constructor initializes object attributes.

Using Objects

Once an object has been instantiated, you can access or modify the object’s attributes or call the object’s methods to perform actions or retrieve information.

Methods

Methods are functions defined within a class that operate on the data (attributes) of an object. Methods can modify object state, perform calculations or operations, and return values

Calling Methods

  • To call a method on an object (in JavaScript), use the object’s name followed by a dot (.), the method name, and parentheses.
  • Pass any required arguments (parameters) inside the parentheses.

Parameters

Methods can take inputs called parameters (or arguments) to process data or customize behavior. When calling a method, parameters are passed to the method by placing them within the parentheses.

Return Values

Methods can produce and transmit results from calculations or operations to the caller for further use via return values. The methods “return values.”