Lesson 9: A Gentle Introduction to Objects

By Maria Antonietta Perna

Object Oriented Programming (OOP) is a programming model used by most contemporary programming languages. JavaScript offers significant object oriented capabilities, has its own built-in objects, and offers you the option of creating your own custom objects.

This is a wide and complex subject and we're only going to scratch the surface in this tutorial series. However, because as soon as you start coding in JavaScript you can't avoid dealing with objects, it's important that you get familiar with some core concepts of OOP.

This short lesson is introductory to the more detailed and practical lessons on specific JavaScript objects that follow.

Here is what you will learn:

  • the concept of object in web programming;
  • what object properties are;
  • what object methods are.

What is an object in Geeky talk?

Real world objects are things like books, cars, balls, etc. We humans deal with the world around us by interacting with and manipulating things, that is, objects.

What most computer programs do is manipulate, manage, and reuse data of various kinds. Therefore, using the real world object metaphor, contemporary programming languages like JavaScript deal with their virtual environment by populating it with packaged data modelled after the concept of an object.

Everything in JavaScript is an object and you've already used plenty of objects without realizing it. In fact, you're already familiar with:

  • the Document object that you used to print your JavaScript messages on the web page;
  • the Math object, that you used to generate random numbers and round off decimals;
  • HTML elements like the button object that you used to manipulate its value attribute;
  • the Date object that you used to retrieve the current date.

Properties and methods

But how exactly does JavaScript manipulate objects?

The answer is: not much differently from the way we humans manipulate real world objects. We do this by interacting with the qualities and capabilities that belong to individual objects.

Let's take a ball as our example. We interact with the ball by means of some of its qualities (its roundness, its hardness, its size, etc.). Also, we interact with the ball on the bais of what we expect the ball's behavior to be like (we expect that we can launch the ball without breaking it, that we can throw it against a wall and it bounces back, etc.).

Similarly, a JavaScript script interacts with its object-modelled data by means of the objects' qualities and behavior. These are called properties and methods.

Properties are values associated with an object. For example, an HTML element object has a value property (like the button object you're familiar with), and an innerHTML property, that you used to add new mark-up to the web page.

Methods represent what an object can do, its behavior, and are very much like functions.

How do you associate an object with a property or a method?

If you wondered what that odd-looking ( . ) dot notation in document.write() or Math.random(), and so on, meant in previous lessons, here's the answer.

You use the object.property and object.method syntax to interact with JavaScript objects.

	
	  //use the random() method of the Math object
	
	  Math.random();
	
	  //use the write() method of the document object
	
	  document.write();
	
	  //use the length property of the string object
	
	  myStringText.length;
	
	  

Summary

This lesson has been a short introduction to the concept of JavaScript objects, their properties and methods.

In the next lesson you will learn about the most widely used properties and methods of the String object.


Related topics in the RepliesViews
No related topics yet

+ Post a new topic


<< Lesson 8: Package your JavaScript Code with Functions

Lesson 10: JavaScript Objects: Strings >>