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:

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) 2013 by Dr. Herong Yang, 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.org";

   // 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

Table of Contents

 About This Book

 Introduction to JavaScript

 ECMAScript Language Specification and JavaScript Dialects

 Data Types, Variables and Expressions

 Flow Control Statements

 Creating, Accessing, and Manipulating Arrays

 Defining and Calling Functions

 Web Browser Supporting JavaScript

 Server-Side and Client-Side Web Scripting

Introduction to Objects

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

 Defining Your Own Object Types

 Inheritance of Properties and Methods through the Prototype Object Chain

 'jrunscript' - JavaScript Shell Command from JDK

 Using Functions as "Function" Objects

 Introduction to Built-in Object Types

 W3C's Document Object Model (DOM) Specifications

 References

 PDF Printing Version