What is an Object?

Objects are a way that Javascript, and most programming languages, allow you to organize and use data.

Objects have data similar to variables called properties and behaviors called methods.

You can think of objects as the programming equivalent of what exists in the physical world such as a microwave oven, a person or a vehicle. They all have properties and behaviors.

There are many ready made objects available in Javascript. Most important is the document object that allows you to access HTML and CSS. There are also other objects that allow you simplify handing dates and math.

The ready made Javascript objects is a good place to start learning how to use objects.

You learn how to create objects later in the course.

1. How objects are used.

Objects are another data type.

Like numbers, strings and booleans, objects are a data type.

Objects have a name

Object names are the same as variable names. Some names of the ready made objects in Javascript are document, window and location.

Objects have an interface

Objects have an interface. The interface is how you use the object. The interface includes data called properties and behaviors called methods.

Properties

Properties are the data for an object.

Properties have a name the same as variables. For example speed, firstName and isRegistered.

Properties, like variables, represent data in memory. Properties, different from variables, can control the access to the data. A property can make the data write once or read only for example.

Physical world objects have properties.

The microwave oven for example has the time for display, its dimensions, its wattage and capacity as properties. Some properties like the time display or cooking duration you can change. Other properties like its dimensions you cannot change.

Properties, like variables, can store any data type including numbers, strings and booleans and objects.

Methods

Methods define the functionality of an object.

Methods are the same as Javascript functions. You learn more about Javascript functions later in the course.

Methods have names with parenthesis as a suffix. For example start(), on() and stop().

Methods may use or require input. Input is supplied through arguments. Arguments are a comma separated list of expressions inside the parenthesis. For example setColor("blue"), setSpeed(100) and addChatMember(handle, isPresenter).

Physical world objects have methods.

You can compare a behavior to what happens when you press buttons on your microwave oven to start and stop cooking.

Methods may return data. In this sense they are data. For example getColor(), getSpeed(100) and getChatMembers(isPresenter).

Using dot notation

The object interface is accessed using dot notation. The object name is followed by a period and then the name of the property or method. For example …

car.startEngine();
car.stopEngineOff();
car.direction('reverse');
car.speed(5);
car.honk(3);
car.fuelLevel;
car.fuelLevel >= 5;

2. The "Built-In" Objects.

Javascript provides you with access to "built-in" objects that can assist your programming work.

Standard built in Javascript objects.

The standard built in Javascript objects cover many categories. In this course we look at those that help using numbers, dates, and mathematical calculations.

  • Math. Handles items such as rounding, trigonometry functions, random numbers and common math functions such as square roots.

  • Date. The Date function allows you to create date object instances. This represents a date and lets you access the elements of a date such as month, year, hour, seconds, milliseconds and perform math and comparisons on the date.

  • Number. It can be used to convert to an from other data types and formats such as convert from a string or from a Date object to a number.

  • String. Allows you to work with string data such as convert the case to upper or lower, search a string, extract parts of a string, pad a string, get the total characters in a string and much more.

  • Array. Arrays are lists of data. Various operations such as sort the list and merge lists.

  • There are many more that you can review at here.

Objects provided by the web browser for Javascript to use

The web browser provided objects that are independent of the Javascript programming language. You use them to access the web browser data and the document information.

  • window. Represents the browser's window. For example you can access the dimensions of the browser window.

  • document. This gives you access to the HTML elements and the CSS in the web page.

  • navigator. This provides access to the web browser. For example you could find out the name the web browser's vendor.

  • location. This provides access to the web browser. For example you could find out the name the web browser's vendor.

  • history. This provides access to the web browser. For example you could find out the name the web browser's vendor.

  • screen. This contains information about the user's screen such as width, height, color depth and pixel depth.

  • console. The Console object provides access to the web browser's developer tools console window. In particular we use the log method console.log.

The web browser provided objects are independent of the Javascript programming language.

How are the built in objects created?

Some of the built in objects are ready to use. All the web browser objects are ready to use such as document. The Math object is an example of a ready to use Javascript "built in" object.

Some of the built in objects are constructors that create individual instances because you may need more than one with different values.

The Date object is an example of constructor object. You may need to work with more than one date in your programming such determining the span of time between a start date and an end date.

Constructor objects are functions. You learn more about Javascript functions later in the course.

Complete and Continue