please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
Next
 

Object.valueOf() Method

Name

Object.valueOf() Method---the primitive value of the specified object

Availability

Navigator 3.0

Synopsis

object.valueOf();

Arguments

None.

Returns

The primitive value associated with the object, if any. Otherwise, returns the object itself.

Description

The valueOf() method of an object returns the primitive value associated with that object, if there is one. For objects of type Object, and for the client-side JavaScript objects, there is no primitive value, and this method simply returns the object itself.

For objects of type Number, however, valueOf() returns the primitive numeric value represented by the object. Similarly, it returns the primitive Boolean value associated with a Boolean object, the string associated with a String object, and the function associated with a Function object.

It is rarely, if ever, necessary to invoke the valueOf() method yourself. JavaScript does this automatically whenever an object is used where a primitive value is expected. In fact, because of this automatic invocation of the valueOf() method, it is difficult to even distinguish between primitive values and their corresponding objects. The typeof operator will show you the difference between strings and String objects and functions and Function objects, for example, but in practical terms, you can use them equivalently in your JavaScript code.

The valueOf() methods of the Number, Boolean, String, and Function objects convert these "wrapper" objects to the primitive values they represent. The Object() constructor performs the opposite operation when invoked with a number, Boolean, string, or function argument: it wraps the primitive value in an appropriate object wrapper. JavaScript performs this primitive to object conversion for you in almost all circumstances, so it is rarely, if ever, necessary to invoke the Object() constructor in this way.

Usage

In some circumstances, you may want to define a custom valueOf() method for your own objects. For example, you might define a JavaScript object type to represent complex numbers (a real number plus an imaginary number). As part of this object type, you'd probably define methods for performing complex addition, multiplication, and so on. But you might also want the ability to treat your complex numbers like ordinary real numbers by discarding the imaginary part. You might do something like the following:

Complex.prototype.valueOf =index.html new Function("return this.real");

With this valueOf() method defined for your Complex object type, you could then do things like pass one of your complex number objects to Math.sqrt(), which would compute the square root of the real portion of the complex number.

See Also

"Object", "Object.toString()"


Previous Home Next
Object.toString() Book Index open()

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/java/script/definitive/refp_280.htm, 7KB, , updated: 2019/10/14 15:00, local time: 2024/12/27 14:46,
TOP NEW HELP FIND: 
18.217.228.195:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://linistepper.com/techref/language/java/script/definitive/refp_280.htm"> [Chapter 21] Reference: Object.valueOf()</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?