Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Problem inheriting from core javascript

    I'd like to make a Degree object that inherits from the Number object and uses the Number constructor but adds a .rad() method that returns the value in radians.

    If I do something like:
    Code:
    function Degree(n) {
    	this.valueOf = function() { return n;}
    	this.rad = function() {
    		return this * (Math.PI / 180);
    	}
    }
    it generally works but I don't get Number's methods like toString and toPrecision.

    Any ideas?

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    I'd just add a method to the Number prototype-
    Code:
    Number.prototype.degtoRad=function(){
        return this*(Math.PI/180)
    }
    Last edited by mrhoo; 05-20-2009 at 09:13 PM.

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks! but... Firebug reports:
    Number.prototype.valueOf called on incompatible Object
    for the line return this * (Math.PI / 180);.

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    See if this helps. Interesting read, regardless.

  • #5
    New to the CF scene
    Join Date
    May 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    mrhoo,
    I'm wondering if the n of the Degree constructor is not getting passed to the Number constructor.

  • #6
    New Coder
    Join Date
    Oct 2008
    Location
    Turkey
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post
    Code:
    Degree=function(n){
    var x=new Number(n);
    x.toRadian = function() {
    	return x* (Math.PI / 180);
    };return x;}
    var x=Degree(15.55);
    alert(x.toFixed(2));//output 15.55
    alert(x.toRadian());//0.271..

  • Users who have thanked nightwolfcem for this post:

    stopher (05-20-2009)

  • #7
    New to the CF scene
    Join Date
    May 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    nightwolfcem,

    Woo hoo! I was struggling with .prototype, and .call and pouring over Crockford's stuff but I never though of this.

  • #8
    New to the CF scene
    Join Date
    May 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Bonus points

    For bonus points...
    If you have an equivalent inverse object, Radian, could you have Degree return a Radian and Radian return a Degree?
    As is, Firebug throws "too much recursion." Is there another way to do it?

  • #9
    New Coder
    Join Date
    Oct 2008
    Location
    Turkey
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post
    Code:
    Angle=function(n){
    var x=new Number(n);
    var mod=0;//mod=0--> degree mod=1-->radian
    x.toString=function (){return x;}
    x.valueOf=function (){return Number.prototype.valueOf.apply(x);}
    x.toFixed=function (){return Number.prototype.toFixed.apply(x,arguments);}
    x.toPrecision=function (){return Number.prototype.toPrecision.apply(x,arguments);}
    x.toExponential=function (){return Number.prototype.toExponential.apply(x,arguments);}
    x.degree = function(v) {if(v!=null){x=v;mod=0;}else{ return mod==0?x:x*(180/Math.PI);}}
    x.radian = function(v) {if(v!=null){x=v;mod=1;}else return mod==1?x:x*(Math.PI/180);};
    return x;}
    var y=Angle(30);//enter 30 degree
    alert(y.degree()+" degree= "+y.radian()+" radian");
    y.degree(90);
    alert(y.degree()+" degree= "+y.radian()+" radian");
    y.radian(0.2);
    alert(y);
    y.radian(25.6);
    alert(y.toFixed());
    alert("Y is "+(y.mod==0?"degree ":"radian ")+"= "+y);
    alert(y.degree()+" degree= "+y.radian()+" radian");
    Last edited by nightwolfcem; 05-21-2009 at 12:02 AM.

  • #10
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,379
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by nightwolfcem View Post
    Code:
    x.toPrecision=function (){return Number.prototype.toPrecision.apply(x,arguments);}
    just curious, what is this for?

    It seems to be applying itself.

    unrelated, arguments is not an array, and .apply wants arrays as a second arg, even if some browsers let it slip though...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #11
    New Coder
    Join Date
    Oct 2008
    Location
    Turkey
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by rnd me View Post
    just curious, what is this for?

    It seems to be applying itself.

    unrelated, arguments is not an array, and .apply wants arrays as a second arg, even if some browsers let it slip though...
    sorry my english is some bad.
    arguments carry of array feautires. [params],length and be plus methods
    if you any Array command aplly to arguments,arguments transform real array as array.property.slice(arguments,0)--> this return only real array party of arguments =[params], but not neccesary aplly method aplly agree robust arguments , not out any problem .



    x.toPrecision=function (){return Number.prototype.toPrecision.apply(x,arguments);}??
    i assign x to y with y=Angle(30); (this will be y= return value of Angle=x )
    but there is little problem
    y.degree(90);
    x.degree = function(v) {if(v!=null){x=v;mod=0;}//x transform Number 90
    normal time y reference to x and y may be transform Number 90,but y not Transform 90 Number ,remain Object (30)
    if i use alert(y); //output return 30, not 90 that is related with toString function that for i redefine toString and other functions
    (toPrecision acces to Number.valueOf function for get value of Number)
    if i not redefine toPrecision function y.precesion aplly for 30(y) ,not for 90(x)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •