mozilla

Revision 85717 of Object.prototype.valueOf()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/valueOf
  • Revision title: valueOf
  • Revision id: 85717
  • Created:
  • Creator: Potappo
  • Is current revision? No
  • Comment correct link for fr:

Revision Content

Summary

Returns the primitive value of the specified object

Method of Object
Implemented in: JavaScript 1.1
ECMA Version: ECMA-262

Syntax

valueOf()

Parameters

None.

Description

JavaScript calls the valueOf method to convert an object to a primitive value. You rarely need to invoke the valueOf method yourself; JavaScript automatically invokes it when encountering an object where a primitive value is expected.

By default, the valueOf method is inherited by every object descended from Object. Every built-in core object overrides this method to return an appropriate value. If an object has no primitive value, valueOf returns the object itself, which is displayed as:

[object Object]

You can use valueOf within your own code to convert a built-in object into a primitive value. When you create a custom object, you can override Object.valueOf to call a custom method instead of the default Object method.

Overriding valueOf for custom objects

You can create a function to be called in place of the default valueOf method. Your function must take no arguments.

Suppose you have an object type myNumberType and you want to create a valueOf method for it. The following code assigns a user-defined function to the object's valueOf method:

myNumberType.prototype.valueOf = new Function(functionText)

With the preceding code in place, any time an object of type myNumberType is 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 valueOf method is usually invoked by JavaScript, but you can invoke it yourself as follows:

myNumber.valueOf()

Note

Objects in string contexts convert via the toString method, which is different from String objects converting to string primitives using valueOf. All string objects have a string conversion, if only "{{ mediawiki.external('object type') }}". But many objects do not convert to number, boolean, or function.

See Also

parseInt, toString

{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Object/valueOf", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/valueOf", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Object/valueOf" } ) }}

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>Returns the primitive value of the specified object
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Method of <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object">Object</a></td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.1</td>
</tr>
<tr>
<td>ECMA Version:</td>
<td>ECMA-262</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<p><code>
valueOf()
</code>
</p>
<h3 name="Parameters"> Parameters </h3>
<p>None.
</p>
<h3 name="Description"> Description </h3>
<p>JavaScript calls the <code>valueOf</code> method to convert an object to a primitive value. You rarely need to invoke the <code>valueOf</code> method yourself; JavaScript automatically invokes it when encountering an object where a primitive value is expected.
</p><p>By default, the <code>valueOf</code> method is inherited by every object descended from <code>Object</code>. Every built-in core object overrides this method to return an appropriate value. If an object has no primitive value, <code>valueOf</code> returns the object itself, which is displayed as:
</p>
<pre>[object Object]
</pre>
<p>You can use <code>valueOf</code> within your own code to convert a built-in object into a primitive value. When you create a custom object, you can override <code>Object.valueOf</code> to call a custom method instead of the default <code>Object</code> method.
</p>
<h4 name="Overriding_valueOf_for_custom_objects"> Overriding <code>valueOf</code> for custom objects </h4>
<p>You can create a function to be called in place of the default <code>valueOf</code> method. Your function must take no arguments.
</p><p>Suppose you have an object type <code>myNumberType</code> and you want to create a <code>valueOf</code> method for it. The following code assigns a user-defined function to the object's <code>valueOf</code> method:
</p>
<pre>myNumberType.prototype.valueOf = new Function(functionText)
</pre>
<p>With the preceding code in place, any time an object of type <code>myNumberType</code> is used in a context where it is to be represented as a primitive value, JavaScript automatically calls the function defined in the preceding code.
</p><p>An object's <code>valueOf</code> method is usually invoked by JavaScript, but you can invoke it yourself as follows:
</p>
<pre>myNumber.valueOf()
</pre>
<h4 name="Note"> Note </h4>
<p>Objects in string contexts convert via the <code><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object/toString">toString</a></code> method, which is different from <code>String</code> objects converting to string primitives using <code>valueOf</code>. All string objects have a string conversion, if only "<code>{{ mediawiki.external('object <i>type</i>') }}</code>". But many objects do not convert to number, boolean, or function.
</p>
<h3 name="See_Also"> See Also </h3>
<p><a href="en/Core_JavaScript_1.5_Reference/Global_Functions/parseInt">parseInt</a>,
<a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object/toString">toString</a>
</p>
<div class="noinclude">
</div>
{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Object/valueOf", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/valueOf", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Object/valueOf" } ) }}
Revert to this revision