Browser/User Agent Support
| IE | Mozilla | Netscape | Opera | Safari | 4.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|---|
Constructors
| Constructor | IE | Mozilla | Netscape | Opera | Safari |
|---|---|---|---|---|---|
Creates a new instance of an Object. | 4.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Properties
| Property | IE | Mozilla | Netscape | Opera | Safari |
|---|---|---|---|---|---|
Specifies the function that creates a prototype of an object. | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Represents the prototype for this class. You can use the prototype to add properties or methods to all instances of a class. | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Methods
| Method | IE | Mozilla | Netscape | Opera | Safari |
|---|---|---|---|---|---|
Deprecated. Evaluates a string of JavaScript code in the context of an object. | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Checks whether a property is inherited. | 5.5+ | 1.0+ | 6.0+ | 7.0+ | no |
Returns true if the object is a prototype of another. | 5.5+ | 1.0+ | 6.0+ | 7.0+ | no |
Returns true if the property can be enumerated in a for/in loop. | 5.5+ | 1.0+ | 6.0+ | 7.0+ | no |
Returns a localized string representation of an object. | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Returns a string representing the source code of the object. | no | 1.0+ | 4.06+ | no | no |
Returns a string representing the specified object. | 4.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Removes a watchpoint set with the watch method. | no | no | 4.0+ | 7.0+ | no |
Returns the primitive value of the specified object | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Watches for a property to be assigned a value and runs a function when that occurs. | no | no | 6.0+ | 7.0+ | no |
References
Array | Boolean | Function | Function.prototype | Number | String
Availability
JavaScript objectsJavaScript 1.0 | JScript 1.0 | ECMAScript v1
Constructor Detail
ObjectNumberStringBoolean Object([String value])
Creates a new instance of an Object.
| String | value | Specifies a JavaScript primitive type (String, Number, or Boolean) to be converted to a String, Number, or Boolean object. (optional) |
Property Detail
Function constructor
Specifies the function that creates a prototype of an object.
- Remarks
- See Also
- Availability
JavaScript 1.1 | JScript 2.0 | ECMAScript v1
Object prototype
Represents the prototype for this class. You can use the prototype to add properties or methods to all instances of a class.
- Remarks
- See Also
- Availability
JavaScript 1.1 | ECMA-262
Method Detail
eval(string string) : NumberObjectString
Deprecated. Evaluates a string of JavaScript code in the context of an object.
| string | string | Any string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects. |
- Remarks
The
evalmethod is not longer available as a method ofObject. Use the top-levelevalfunction.Backward Compatibility
JavaScript 1.2 and 1.3
evalas a method ofObjectand every object derived fromObjectis deprecated (but still available).JavaScript 1.1
evalis a method ofObjectand every object derived fromObject.- See Also
- Availability
JavaScript 1.1 | Deprecated as a method of objects; retained as a top-level function in JavaScript 1.2. | Removed as a method of objects in JavaScript 1.4
hasOwnProperty(String propname) : Boolean
Checks whether a property is inherited.
| String | propname | The name of the property to check for. |
- See Also
- Availability
JavaScript 1.5 | JScript 5.5 | ECMAScript v3
isPrototypeOf(Object object) : Boolean
Returns true if the object is a prototype of another.
| Object | object | The object to compare the original object to. |
- See Also
- Availability
JavaScript 1.5 | JScript 5.5 | ECMAScript v3
propertyIsEnumerable(String property) : Boolean
Returns true if the property can be enumerated in a for/in loop.
| String | property | Name of the property to check. |
- See Also
- Availability
JavaScript 1.5 | JScript 5.5 | ECMAScript v3
toLocaleString() : String
Returns a localized string representation of an object.
- See Also
Array.toLocaleString | Date.toLocaleString | Number.toLocaleString | Object.toString
- Availability
JavaScript 1.5 | JScript 5.5 | ECMAScript v3
toSource() : String
Returns a string representing the source code of the object.
-
Example: Using
toSourceThe following code defines the
Dogobject type and createstheDog, an object of typeDog:function Dog(name,breed,color,sex) { this.name=name this.breed=breed this.color=color this.sex=sex } theDog = new Dog("Gabby","Lab","chocolate","girl")Calling the
toSourcemethod oftheDogdisplays the JavaScript source that defines the object:theDog.toSource() //returns ({name:"Gabby", breed:"Lab", color:"chocolate", sex:"girl"}) - Remarks
-
The
toSourcemethod returns the following values:- For the built-in
Objectobject,toSourcereturns the following string indicating that the source code is not available:
function Object() { [native code] }- For instances of
Object,toSourcereturns a string representing the source code. - For custom objects,
toSourcereturns the JavaScript source that defines the object as a string.
This method is usually called internally by JavaScript and not explicitly in code. You can call
toSourcewhile debugging to examine the contents of an object. - For the built-in
- See Also
- Availability
JavaScript 1.3
toString() : String
Returns a string representing the specified object.
-
The following example prints the string equivalent of the current location.
document.write("location.toString() is " + location.toString() + "
")The output is as follows:
location.toString() is file:///C|/TEMP/myprog.htmll
Assume you have an
Imageobject namedsealifedefined as follows:<IMG NAME="sealife" SRC="images\seaotter.gif" ALIGN="left" VSPACE="10">
Because the
Imageobject itself has no specialtoStringmethod,sealife.toString()returns the following:object Image
The following example prints the string equivalents of the numbers 0 through 9 in decimal and binary.
for (x = 0; x < 10; x++) { document.write("Decimal: ", x.toString(10), " Binary: ", x.toString(2), "
") }The preceding example produces the following output:
Decimal: 0 Binary: 0 Decimal: 1 Binary: 1 Decimal: 2 Binary: 10 Decimal: 3 Binary: 11 Decimal: 4 Binary: 100 Decimal: 5 Binary: 101 Decimal: 6 Binary: 110 Decimal: 7 Binary: 111 Decimal: 8 Binary: 1000 Decimal: 9 Binary: 1001
- Remarks
-
Every object has a
toStringmethod that is automatically called when it is to be represented as a text value or when an object is referred to in a string concatenation. For example, the following examples requiretheDogto be represented as a string:document.write(theDog) document.write("The dog is " + theDog)By default, the
toStringmethod is inherited by every object descended fromObject. You can override this method for custom objects that you create. If you do not overridetoStringin a custom object,toStringreturns[object type], wheretypeis the object type or the name of the constructor function that created the object.For example:
var o = new Object() o.toString // returns [object Object]
- See Also
- Availability
JavaScript 1.0 | JScript 2.0 | ECMAScript v1
unwatch(String prop) : Object
Removes a watchpoint set with the watch method.
| String | prop | The name of a property of the object. |
- Remarks
By default, this method is inherited by every object descended from
Object.- See Also
- Availability
JavaScript 1.2
valueOf() : Object
Returns the primitive value of the specified object
- Remarks
-
JavaScript calls the
valueOfmethod to convert an object to a primitive value. You rarely need to invoke thevalueOfmethod yourself; JavaScript automatically invokes it when encountering an object where a primitive value is expected.By default, the
valueOfmethod is inherited by every object descended fromObject. Every built-in core object overrides this method to return an appropriate value. If an object has no primitive value,valueOfreturns the object itself, which is displayed as:object Object]
You can use
valueOfwithin your own code to convert a built-in object into a primitive value. When you create a custom object, you can overrideObject.valueOfto call a custom method instead of the defaultObjectmethod.Overriding
valueOffor custom objectsYou can create a function to be called in place of the default
valueOfmethod. Your function must take no arguments.Suppose you have an object type
myNumberTypeand you want to create avalueOfmethod for it. The following code assigns a user-defined function to the object'svalueOfmethod:myNumberType.prototype.valueOf = new Function(functionText);
With the preceding code in place, any time an object of type
myNumberTypeis used in a context where it is to be represented as a primitive value, JavaScript automatically calls the function defined in the preceding code.An object's
valueOfmethod is usually invoked by JavaScript, but you can invoke it yourself as follows:myNumber.valueOf();
Note
Objects in string contexts convert via the
toStringmethod, which is different fromStringobjects converting to string primitives usingvalueOf. All string objects have a string conversion, if only "[object type]". But many objects do not convert to number, boolean, or function. - See Also
- Availability
JavaScript 1.1 | JScript 2.0 | ECMAScript v1
watch(String prop, String handler) : Object
Watches for a property to be assigned a value and runs a function when that occurs.
| String | prop | Name of the property to set. |
| String | handler | Name of the function to call. |
Example: Using
watchandunwatchThis script displays the following:
o.p changed from 1 to 2 o.p changed from 2 to 3 o.p changed from undefined to 4
- Remarks
-
Watches for assignment to a property named
propin this object, callinghandler(prop, oldval, newval)wheneverpropis set and storing the return value in that property. A watchpoint can filter (or nullify) the value assignment, by returning a modifiednewval(oroldval).If you delete a property for which a watchpoint has been set, that watchpoint does not disappear. If you later recreate the property, the watchpoint is still in effect.
To remove a watchpoint, use the
unwatchmethod. By default, thewatchmethod is inherited by every object descended fromObject.The JavaScript debugger has functionality similar to that provided by this method, as well as other debugging options. For information on the debugger, see Venkman.
In NES 3.0 and 4.x,
handleris called from assignments in script as well as native code. In Firefox,handleris only called from assignments in script, not from native code. For example,window.watch('location', myHandler)will not callmyHandlerif the user clicks a link to an anchor within the current document. However, the following code will callmyHandler:window.location += '#myAnchor';. - See Also
- Availability
JavaScript 1.2