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

Objects of "Object" Data Type

This section provides a quick description of the 'Object' data type, its properties and methods. A tutorial example is provided on how to create and test an object of the 'Object' data type.

To learn how to use objects in JavaScript, we need to start with the "Object" data type, which has the following basic features:

  • All "prototype" data types are extended from the "Object" data type.
  • All objects of all "prototype" data types are inheriting properties and methods from the "Object" data type.
  • Objects of the "Object" data type can be created with the object initializer operator ({}).
  • "Object" data type has two properties: "constructor", and "prototype"
  • "Object" data type has a number of methods: "hasOwnProperty()", "isPrototypeOf()", "toString()", "valueOf()".

Here is tutorial example showing some of those properties and methods defined on "Object" data type:

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

   // Creating an object and assigning it to a variable
   var mySite = new Object();
   
   // Showing the built-in properties
   document.writeln('mySite.constructor: '+mySite.constructor); 
   document.writeln('mySite.prototype: '+mySite.prototype); 

   // Adding a property this object only
   mySite.name = "herongyang.com";

   // Converting this object to a string
   document.writeln('mySite.toString(): '+mySite.toString()); 

   // Converting this object to a numeric value
   document.writeln('mySite.valueOf(): '+mySite.valueOf());
   
   // Testing a property of this object
   document.writeln('mySite.hasOwnProperty("name"): '
      +mySite.hasOwnProperty("name"));
   document.writeln('mySite.hasOwnProperty("constructor"): '
      +mySite.hasOwnProperty("constructor"));
   document.writeln('mySite.hasOwnProperty("address"): '
      +mySite.hasOwnProperty("address"));
</script>
</pre>
</body>
</html>

Run this example, you will get this interesting result:

mySite.constructor: 
function Object() {
    [native code]
}

mySite.prototype: undefined
mySite.toString(): [object Object]
mySite.valueOf(): [object Object]
mySite.hasOwnProperty("name"): true
mySite.hasOwnProperty("constructor"): false
mySite.hasOwnProperty("address"): false

Explanation of this result will be provided in next sections.

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
Objects of "Object" Data Type