METHOD:
Object::valueOf
Object.valueOf()
The valueOf method returns a
primitive value for a specified object and is inherited by all objects descended from
Object. It is usually called automatically by JavaScript behind the scenes whenever it
encounters an object where a primitive value is expected. If the object has no
primitive value, then the object itself is returned as [object Object]. You can also call
valueOf yourself to convert a built-in object into a primitive value. The following
two examples illustrate uses of it:
Code:
(Object.valueOf()
Output:
function Object() { [native code] }
Code:
function Cat(breed, name, age)
{
this.breed = breed
this.name = name
}
Cat.valueOf()
Output:
function Cat(breed, name, age) { this.breed = breed this.name = name }
The valueOf method can also be overwritten in a custom object by assigning a user-defined
function with no arguments in its place as follows:
Code:
Cat.prototype.valueOf() = myValueOf()
NOTE:
Every core JavaScript object will over-ride the valueOf method to return an
appropriate value.
|