Search⌘ K
AI Features

Programming with Objects

Explore how to build JavaScript objects and define methods through an engaging mini RPG example. Understand object properties, methods, and the 'this' keyword to grasp fundamental object-oriented programming concepts in JavaScript.

Many books and courses teach object-oriented programming through examples involving animals, cars or bank accounts. Let’s try something cooler and create a mini-role playing game (RPG) using objects. In a role-playing game, each character is defined by many attributes like strength, stamina or intelligence. Here’s the character screen of a very popular online RPG.

Show online RPG manu
Show online RPG manu

In our simpler example, a character will have three attributes:

  • her name,
  • her health (number of life points),
  • her strength.

A naive example

Let me introduce you to Aurora, our first RPG character.

Javascript (babel-node)
const aurora = {
name: "Aurora",
health: 150,
strength: 25
};

The aurora object has three properties: name, health and strength.

As you can see, you can assign numbers, strings, and even other objects to properties!

Aurora is about to start a series of ...