Javascript objects
Javascript is an object-oriented language.
An object-oriented language is a language where data types of data structures are defined by the programmer as well as their properties and the things that can be done with them.
This tutorial focuses on:
- What is an object?
- Object properties
- Object methods
- Javascript's built-in objects
- Building your own objects
- Using objects
What is an object?
An object is a special data type that does not simply store a single value, but instead is an entire data structure used to represent something. For example, if you were to create an object that represents a vehicle, you can create a "Vehicle" object.
Object properties
An object's properties are the details about it. For example, a "Vehicle" object would have properties such as model, year, color, and mileage.
Object methods
An object's methods are the things that can be done with the object or the things an object can do. For example, a "Vehicle" object would have methods such as drive, reverse, open door, and park.
Javascript's built-in objects
Javascript has several built-in objects that can be used to print text, perform calculations, and more.
Some of Javascript's built-in objects:
- String - One of the most fundamental objects in Javascript. The String object is used to print and manipulate text in several ways such as returning part of a text string, returning the length of a text string, and more.
- String object properties:
- length - Returns the length of a text string.
- String object methods:
- charAt() - Returns a character from a text string at a specified position.
- concat() - Joins together two or more text strings.
- replace() - Replaces some characters with a set of other characters in a text string.
- toLowerCase() - Displays a text string in all lowercase letters.
- Math - Used to perform various mathematical calculations such as returning the absolute value of a number, returning a random number, and more.
- Math object properties:
- PI - Returns the value of PI (3.14)
- Math object methods:
- abs() - Returns the absolute value of a number.
- random() - Returns a random number between 0 and 1.
- min() - Returns the lesser of two numbers.
- max() - Returns the greater of two numbers.
Building your own objects
Creating the object
An object is created the same way a function is created. When creating an object, the name of the function should be the name the object will be referred to as.
The above example creates a Vehicle object.
Specifying the properties of an object
Using the "Vehicle" object example from above, we can say that some properties of this object include model, year, color, and mileage. To apply these properties to our Vehicle object, we would place them right in the object template.
Within the object template, you can assign values to the properties of the object with this.propertyName. The this keyword signifies the current instance of the object.
Specifying the methods of an object
Using the Vehicle object, we can say that some methods of it are drive, reverse, openDoor, and park. An objects methods are declared just as functions are.
The above example creates a drive() method for the Vehicle object. Using this method, we can specify how many miles to drive and add that number to the total mileage of the car.
NOTE: Methods of an object should be added to the object template!
The above example demonstrates adding the drive() method to the Vehicle object template.
Using objects
Using Javascript's built-in objects
Instantiation
To use one of Javascript's built-in objects, you have to instantiate it using the new keyword and the objects name.
Using properties
To use the properties of Javascript's built-in functions, you have to use the name of the instance of the object, followed by a ., followed by the name of the property.
Using methods
To use the methods of Javascript's built-in objects, you have to do so using the name of the instance of the object, followed by a ., followed by the name of the method, followed by parenthesis and the parameters that come with the method, if any.
NOTE: The Math class cannot be instantiated! Instead, the name of the class itself is used together with its properties and methods.
Using your own objects
Instantiation
To use one of your own objects, you have to instantiate it using the new keyword and the objects name, the same way as with Javascript's built-in objects.
Using properties
To use the properties of one of your own objects, you have to use the name of the instance of the object, followed by a ., followed by the name of the property.
Using methods
To use the methods of one of your own objects, you have to use the name of the instance of the object, followed by a ., followed by the name of the method, followed by parenthesis and the parameters that come with the method, if any.
In the above example, the drive() method of the Vehicle object is used to add 5 miles to the total number of miles driven by aVehicle. The mileage is printed before and after the method is called to show the difference.