Real Life Objects, Properties, and Methods
In real life, a car is an object.
A car has properties like weight and color, and methods like start and stop:
car.name = Fiat
car.model = 500
car.weight = 850kg
car.color = white
All cars have the same properties, but the property values differ from car to car.
All cars have the same methods, but the methods are performed at different times.
This code assigns a simple value (Fiat) to a variable named car:
Objects are variables too. But objects can contain many values.
This code assigns many values (Fiat, 500, white) to a variable named car:
The values are written as name:value pairs (name and value separated by a colon).
Methods are actions that can be performed on objects.
Methods are stored in properties as function definitions.
Spaces and line breaks are not important. An object definition can span multiple lines:
Accessing Object Properties
You can access object properties in two ways:
Accessing Object Methods
You access an object method with the following syntax:
If you access the fullName method, without (), it will return the function definition:
A method is actually a function definition stored as a property value.
Do Not Declare Strings, Numbers, and Booleans as Objects!
var y = new Number(); // Declares y as a Number object
var z = new Boolean(); // Declares z as a Boolean object
Avoid String, Number, and Boolean objects. They complicate your code and slow down execution speed.
You will learn more about objects later in this tutorial.