Saturday, November 2, 2024

The JavaScript Diaries: Part 6

This week we are going to go a little deeper into our study, a process that
will last for several weeks. I want to make sure you fully grasp this subject
as it’s a very important topic, but it can be challenging to understand in the
beginning. Take your time, read carefully, and contemplate what you’ve learned.
Eventually, you’ll gain an understanding.

JavaScript Objects

As I mentioned in the first
installment
, JavaScript is an object-based scripting language.
This means that the JavaScript interpreter sees the existing data structure
as objects rather than a bunch of scattered, random items. As defined by Webopedia
an object is “generally, any item that can be individually selected and manipulated….
In object-oriented programming, for example, an object is a self-contained entity
that consists of both data and procedures to manipulate the data.” Objects can
refer to HTML elements, certain parts of the browser, and often refer to custom-made
objects. For the most part, the code that is written is used to control objects.

In an object, related properties and methods are grouped into a single package,
hence the term “object.” Like a function, an object allows you to organize related
items within a script. In fact, a function is an object and can be thought of
as a ‘thing.’ A good example of this is a car, which is an object and
a thing. An example of an object is:

  function getObject(param1,param2,param3) {
    this.param1=param1;
    this.param2=param2;
    this.param3=param3;
  }

Object Properties

An object has properties. A property is a component or part
of the object. Using the above example, a car’s properties may include a radio
and tires. Properties are data containers, just like variables. Besides being
a part of an object, they also give information about the object. One important
difference is that properties give information about the window object whereas
variables do not. An object holds properties that can be accessed from the outside
for use in the overall script.

The properties of an object are accessed through the use of a dot operator.
The format would be the name of the object, followed by the dot operator, followed
by the property you are trying to access: car.radio and car.tires.
You can even go to deeper and obtain further properties, such as: car.cdplayer.speakers.wires.

Object Methods

An object can also have methods. A method does something
to the object or with the object, just like the car, which can go forward and
backward. A method may cause a new browser window to open, or it may cause text
to be selected. Methods use parentheses just like functions, i.e. mainObject.getPrice().
A main difference, however, is that methods can act on a window object whereas
functions cannot. According to Core JavaScript Guide 1.5: “A method
is a function associated with an object. You define a method the same way you
define a standard function.”

Review

Let’s review what we’ve just covered. An object is a thing, a collection
of properties grouped together. Properties are individual parts of
the object. A method is something that causes the object to do something.
It may help to think of objects and properties as nouns and methods as verbs.

Now, let’s take a look at objects in greater detail. First, we’ll look at
how to create your own. Later, we’ll look at the ones that are built into the
JavaScript language.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured