| Refresh | Home EGTry.com

convert object to number, provide a numeric representation of object


commentsJavascript sourceExecution result in your current browser
use valueOf in numeric context
   document.write(Object.prototype.valueOf);
   function Circle(radius) {
     this.r=radius;
    }
    Circle.prototype.valueOf=function() {
      return this.r;
     }
    var circle1=new Circle(10);
    var circle2=new Circle(20);
    var sum1=circle1+circle2;
    var sum2=circle1.valueOf()+circle2.valueOf();
   
    document.write("<br/>"+sum1);
    document.write("<br/>"+sum2);