JavaScript Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 2.10

What Is an Object?

This chapter provides introductions and tutorial examples on the 'object' data type in JavaScript. Topics include creating 'Object' objects, adding and retrieving properties, adding and running methods, named and indexed properties, comparing arrays with objects.

What Is an Object? - An object is an instance of a special data type called "Object" or any "prototype" data type.

JavaScript supports objects with some interesting features:

  • An object is instantiated with from a "prototype", not from a "class" or a "struct" like other languages.
  • Objects of the "Object" data type can be created with the object initializer operator ({}).
  • Objects of the "Object" data type and any "prototype" data type can be created with the constructor operator (new).
  • When an object is assigned to a variable, a reference of the object is stored in the variable. This is different than assigning a primitive value to a variable, where the value itself is stored in the variable.
  • An object can only have public properties and public methods.
  • Properties and methods can be accessed with the dot (.) operator.
  • An object in JavaScript is equivalent to an associate array in other languages.
  • Properties and methods can be accessed with the array operator ([]).
  • New properties and methods can be added to all objects of a single prototype dynamically.
  • Existing properties and methods can be removed from all objects of a single prototype dynamically.
  • New properties and methods can be added to one object and not to other objects of the same prototype dynamically.
  • Existing properties and methods can be removed from one objects and not from other objects of the same prototype dynamically.
  • JavaScript provided a couple of built-in user defined data types, Array, Boolean, Date, Function, Math, Number, RegExp, and String.

To show you some of these feature, I wrote this simple tutorial example to create an object of "Object" data type, and add a new property on this new object:

<html>
<!-- Object_Data_Type_and_Value.html
   Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/
-->
<head>
<title>Object Data Type and Value</title>
</head>
<body>
<pre>
<script type="text/javascript">

   // Assigning a primitive value to a variable
   var jsSite = "mozilla.orgm";

   // Creating an object and assigning it to a variable
   var mySite = new Object();
   
   // Adding a new property to one object
   mySite.name = "herongyang.com";
   
   // Retrieving the value of object property
   document.writeln('"mySite.name" property: '+mySite.name); 

   // Checking the data type of mySite
   document.writeln('"mySite" data type: '+(typeof mySite)); 

   // Checking the data type of jsSite
   document.writeln('"jsSite" data type: '+(typeof jsSite)); 
</script>
</pre>
</body>
</html>

Run this example, you will get:

"mySite.Name" property: herongyang.com
"mySite" data type: object
"jsSite" data type: string

Sections in This Chapter

What Is an Object?

Objects of "Object" Data Type

Adding and Deleting Object Own Properties

Adding and Deleting Object Own Methods

Using "this" Keyword to Represent Current Object

Object Literals of the "Object" Type

Objects and Associate Arrays

Objects with Indexed Properties

Differences between "Object" and "Array"

Using "Array" Objects as "Object" Objects

Dr. Herong Yang, updated in 2008
What Is an Object?