Availability: |
| ||||||||
JavaScript syntax: | - | myObject = new Object() | |||||||
- | myObject = Object | ||||||||
Object properties: | __parent__, __proto__, constructor, name, prototype | ||||||||
Object methods: | assign(), eval(), hasOwnProperty(), isPrototypeOf(), propertyIsEnumerable(), toLocaleString(), toSource(), toString(), unwatch(), valueOf(), watch() |
An instance of the class "Object" is created by using the new operator on the Object() constructor. The new object adopts the behavior of the built-in prototype object through the prototype-inheritance mechanisms.
All properties and methods of the prototype are available as if they were part of the instance.
An Object is a member of the type Object. It is an unordered collection of properties, each of which may contain a primitive value, another object, or a function.
The constructor is invoked by the new operator or by calling it as a Constructor function. For example:
new String("Some Text");
This will create a new object of the String type. You can invoke the constructor without the new operator but the consequences will depend on the constructor as to what it will yield as a result. In the case of the String data type, the constructor could be invoked like this:
String("Some Text");
However, you would get a primitive string as a result from this and not a String object. JavaScript is somewhat forgiving and you may not notice this happening until later on when it becomes important that you have a String object and not a simple string.
Because this object is the topmost parent object in the prototype inheritance hierarchy, all other object classes inherit its methods and properties. However, in some cases they will get overridden or nulled out.
DOM level 2 adds the following properties:
contentDocument
Although JavaScript is object-based, it does not support true object-oriented classes such as the ones you find in C++, Smalltalk, Java, or Objective C. Instead, it provides Constructor mechanisms, which create objects by allocating space for them in memory and assigning initial values to their properties.
All functions, including constructors, are themselves objects. However, not all objects are constructors. Each constructor has a Prototype property that is used to facilitate inheritance based on the prototype. It also provides for shared properties which is similar to but not the same as the Class properties that you find in true object-oriented languages.
Externally, the objects in JavaScript exhibit most of the attributes of a class-based object-oriented system and some commentators argue that this qualifies JavaScript as being a genuine object-oriented system. However I think the following points declassify it as a truly object-oriented system, meaning that it is an "object-like" system:
Global variables and the scope chain mechanism
Prototype based inheritance
Creation of multiple objects and calling them within a single script
Object data is not truly private
It's a close enough call that JavaScript 2.0 may well move it into the class-based object-oriented category at which time the prototype inheritance would be replaced with super-class/sub-class mechanisms and the arguments become null and void.
Be very careful not to confuse this generic top-level core object with the object that MSIE instantiates to represent an <OBJECT> tag. MSIE creates OBJECT objects for that purpose but also supports Object objects. For this reason, it may be the case that interpreters cannot become case-insensitive when matching class names. If they did, then it would be impossible to distinguish between Object and OBJECT class names.
Property | JavaScript | JScript | N | IE | Opera | NES | ECMA | DOM | CSS | HTML | Notes |
---|---|---|---|---|---|---|---|---|---|---|---|
__parent__ | 1.2 ![]() | ![]() | 4.0 ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | - |
__proto__ | 1.2 ![]() | ![]() | 4.0 ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | - |
constructor | 1.1 ![]() | 3.0 ![]() | 3.0 ![]() | 4.0 ![]() | 3.0 ![]() | ![]() | 2 ![]() | ![]() | ![]() | ![]() | - |
name | ![]() | 5.5 ![]() | ![]() | 5.5 ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | - |
prototype | 1.1 ![]() | 3.0 ![]() | 3.0 ![]() | 4.0 ![]() | 3.0 ![]() | ![]() | 2 ![]() | ![]() | ![]() | ![]() | ![]() |
Method | JavaScript | JScript | N | IE | Opera | NES | ECMA | DOM | CSS | HTML | Notes |
---|---|---|---|---|---|---|---|---|---|---|---|
assign() | 1.1 ![]() | ![]() | 3.0 ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
eval() | 1.1 ![]() | 3.0 ![]() | 3.0 ![]() | 4.0 ![]() | 3.0 ![]() | 2.0 ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
hasOwnProperty() | 1.5 ![]() | 5.5 ![]() | 6.0 ![]() | 5.5 ![]() | ![]() | ![]() | 3 ![]() | ![]() | ![]() | ![]() | - |
isPrototypeOf() | 1.5 ![]() | 5.5 ![]() | 6.0 ![]() | 5.5 ![]() | ![]() | ![]() | 3 ![]() | ![]() | ![]() | ![]() | - |
propertyIsEnumerable() | 1.5 ![]() | 5.5 ![]() | 6.0 ![]() | 5.5 ![]() | ![]() | ![]() | 3 ![]() | ![]() | ![]() | ![]() | - |
toLocaleString() | 1.5 ![]() | 5.5 ![]() | 6.0 ![]() | 5.5 ![]() | ![]() | ![]() | 3 ![]() | ![]() | ![]() | ![]() | ![]() |
toSource() | 1.3 ![]() | 3.0 ![]() | 4.06 ![]() | 4.0 ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
toString() | 1.1 ![]() | 3.0 ![]() | 3.0 ![]() | 4.0 ![]() | 3.0 ![]() | ![]() | 2 ![]() | ![]() | ![]() | ![]() | - |
unwatch() | 1.2 ![]() | ![]() | 4.0 ![]() | ![]() | ![]() | 3.0 ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
valueOf() | 1.1 ![]() | 3.0 ![]() | 3.0 ![]() | 4.0 ![]() | 3.0 ![]() | 2.0 ![]() | 2 ![]() | ![]() | ![]() | ![]() | - |
watch() | 1.2 ![]() | ![]() | 4.0 ![]() | ![]() | ![]() | 3.0 ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
ECMA 262 edition 2 - section - 4.2.1
ECMA 262 edition 2 - section - 4.3.3
ECMA 262 edition 2 - section - 10.1.5
ECMA 262 edition 2 - section - 15.2
ECMA 262 edition 3 - section - 4.2.1
ECMA 262 edition 3 - section - 4.3.3
ECMA 262 edition 3 - section - 10.1.5
ECMA 262 edition 3 - section - 15.2
O'Reilly JavaScript Definitive Guide - page - 44
Wrox Instant JavaScript - page - 28
Prev | Home | Next |
Object model | Up | Object() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |