⚠ ️Do not explain classes yet, just javascript simple object literals.
Objects are like a group of functions that together have a greater purpose.
E.g: In real life, a car has a bunch of functionalities that together make them a car:
- A car can accelerate, break, turn on the engine, turn off the engine, etc.
- A car also has shared properties (not only functions), a car is red
with a serial number
with 4 tires
and many other properties.
This will be a Javascript Representation of a car object:
1let car = { 2 color: 'red', 3 serialNumber: 'AKF345', 4 numberOfTires: 4, 5 currentSpeed: 0, 6 accelerate: function(delta){ 7 this.currentSpeed = this.currentSpeed + delta; 8 }, 9 break: function(delta){ 10 this.currentSpeed = this.currentSpeed - delta; 11 } 12}
To access or set an object’s properties, you can do so in the following ways:
1// to set a proparty value 2objectName.propertyName = valueToSet; 3objectName['propertyName'] = valueToSet; //alternate way 4 5//or to retrieve a propery value 6let anyVariable = objectName.propertyName; 7let anyVariable = objectName['propertyName']; //alternate way