This section provides a quick description and a tutorial example script of the 'Object' built-in object type, which is the root or base object type of all other object types.
The "Object" object type is probably the most important built-in object type.
It has the following main features:
"Object" is the root (or base) object type in the object type tree.
All other object types are inherited from the Object object type.
"Object" objects can be created with the Object() constructor function.
"Object" objects can also be created with an object literal: {property: value, ...}.
Here is a tutorial sample script showing you some of those features:
// Object_Object_Type.html
// Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/
// Creating an object of the "Object" object type
var myObject = new Object();
// Adding properties to the object
myObject.name = "Unknown";
myObject.size = "1.71m";
myObject.weigth = "65kg";
// Checking the object
println("About the myObject:");
println(" Type: "+(typeof myObject));
println(" Instance Of Object: "+(myObject instanceof Object));
println(" Properties:");
for (var item in myObject) {
println(" "+item+": "+myObject[item]);
}
// Creating an object of the "Book" object type
var myBook = new Book("JavaScript Tutorial", "Herong Yang");
// Checking the object
println("About the myBook:");
println(" Type: "+(typeof myBook));
println(" Instance Of Object: "+(myBook instanceof Object));
println(" Instance Of Book: "+(myBook instanceof Book));
println(" Properties:");
for (var item in myBook) {
println(" "+item+": "+myBook[item]);
}
// Defining the constructor for a new object type
function Book(title, author) {
this.title = title;
this.author = author;
}
If you run this script with "jrunscript", you will get:
About the myObject:
Type: object
Instance Of Object: true
Properties:
weigth: 65kg
name: Unknown
size: 1.71m
About the myBook:
Type: object
Instance Of Object: true
Instance Of Book: true
Properties:
title: JavaScript Tutorial
author: Herong Yang
"myBook" is an instance of "Book" and "Object", because "Book" is sub type of "Object" by default.
Read other "Object" related chapters to see more tutorial examples.