In Javascript ,valueOf() method is used to wrapped the primitive value of a String object .And this method is automatically call by javascript (from backend ).
Syntax :
stringobj.valuOf( ) // There is no parameter .
In Javascript , this method is used to convert an object to a primitive value and javascript automatically call this method after invoke the valueOf method by user.(when there is primitive value is expected ).By default , this method is inherited by every object form Object .If an object has no primitive value , then this method return the object itself like : [Object Object]
You can override this method by your own method (by calling your custom method instead of the default Object method ).
Example :
myNameType.prototype.valuOf = function ( ) { return customPrimitiveValue ; } ;
myName.valueOf( ); // To call your custom method(override the object valueOf method ) ;
Example of valueOf() method :
name = new Object ( ) ;
myname = name.valueOf ( ) ; [ Object Object ]
Another example :
without valueOf method
var fruit = new fruit(5);<br>
document.write(typeof fruit);//it will return object
with valueOf method
var fruit = new fruit (5);<br>
var name = fruit.valueof( );<br>
document.write(name); // 10<br>
document.write(typeof name); // number
0 Comment(s)