Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 11 of 11

05202009, 04:03 PM #1
 Join Date
 May 2009
 Posts
 5
 Thanks
 1
 Thanked 0 Times in 0 Posts
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); } }
Any ideas?

05202009, 05:23 PM #2
 Join Date
 Mar 2006
 Posts
 730
 Thanks
 36
 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; 05202009 at 10:13 PM.

05202009, 07:44 PM #3
 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 linereturn this * (Math.PI / 180);
.

05202009, 07:52 PM #4
 Join Date
 Jun 2002
 Posts
 1,404
 Thanks
 2
 Thanked 32 Times in 32 Posts
See if this helps. Interesting read, regardless.

05202009, 08:33 PM #5
 Join Date
 May 2009
 Posts
 5
 Thanks
 1
 Thanked 0 Times in 0 Posts
mrhoo,
I'm wondering if then
of theDegree
constructor is not getting passed to theNumber
constructor.

05202009, 09:12 PM #6
 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 (05202009)

05202009, 09:56 PM #7
 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.

05202009, 10:05 PM #8
 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?

05212009, 12:23 AM #9
 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; 05212009 at 01:02 AM.

05212009, 07:52 AM #10
 Join Date
 Jun 2007
 Location
 Urbana
 Posts
 4,618
 Thanks
 11
 Thanked 619 Times in 599 Posts
Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

05212009, 09:46 AM #11
 Join Date
 Oct 2008
 Location
 Turkey
 Posts
 15
 Thanks
 0
 Thanked 1 Time in 1 Post
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)