Classes And Objects

Class Definition

Class is a blueprint in which characteristics (properties) and behaviors (functions) are defined.

An example of class definition is:

In the example:

  • class is the keyword for class defining.
  • Person - the name of the class.

Object Creation

Object is an instance created from a class.

Using the defined class we can create objects and set properties.

Output:

Explanation:

  • At lines 1 - 6 a class is defined.
  • The class has a function introduceYourself that uses the properties of a class object via the this keyword.
  • At line 9 an object is created.
  • At lines 12 and 13 values are set to the members.
  • At lines 15 the function introduceYourself is called and a message is printed.