Availability: |
| ||||||||
Property/method value type: | Object object | ||||||||
JavaScript syntax: | - | new Object | |||||||
- | new Object() | ||||||||
- | new Object(aValue) | ||||||||
Argument list: | aValue | An initial value for the new object |
When Object is called as part of a new expression, it is a constructor that may create an object instance.
There are limitations what is sensible for the new operator to be able to do with the Object constructor. Since the Object is considered to be the highest ancestor of all objects in the prototype inheritance chain, you cannot logically have more than one Object object. Passing other native objects to the Object Constructor implies a type casting from their native object type to the Object type. That's not logical either. The main use of the Object Constructor then is to manufacturer object instantiations from non-object data types.
The table summarizes the resulting values from using the Object() constructor with the new operator.
Value | Result |
---|---|
No argument | Creates a new empty object |
null | Creates a new empty object |
undefined | Creates a new empty object |
Boolean | Creates a new boolean object whose default value is the input value |
Number | Creates a new number object whose default value is the input value |
String | Creates a new string object whose default value is the input value |
Native object | Returns the native object itself |
Host object | Host implementation dependant behavior. Objects are cloned if necessary but some may not be. |
Unless you assign the result of the new operation, an object will simply consume memory. You need to store a reference to it at the time it is instantiated. You can do this by assigning it to a variable or a property of another object, passing it into a function and making sure it gets retained in there, or storing it as an element in an array.
You can refer to objects without the parentheses but then you are not referring to a constructor function but to the object itself. The behavior varies between browsers and depends on the kind of object being instantiated and probably depends on whether it is a wrapper for a primitive data type or a more complex aggregated type.
These all appear to create the same kind of object in both MSIE and Netscape:
myObject = Object();
myNewObject = new Object();
myOtherObject = new Object;
These do not:
myBoolean = Boolean();
myNewBoolean = new Boolean();
myOtherBoolean = new Boolean;
In the case of the Boolean, Number, and String object types, only the first form will initialize the new object with a value. Placing the result of these examples in document.write() statements illustrates the behavior. You may want to examine the objects returned in more detail by developing an object inspector script.
ECMA 262 edition 2 - section - 15.1.1
ECMA 262 edition 2 - section - 15.1.3.1
ECMA 262 edition 2 - section - 15.2.2.2
ECMA 262 edition 3 - section - 15.2.2
Prev | Home | Next |
Object object | 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. |