Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-20-2009, 03:03 PM   PM User | #1
stopher
New to the CF scene

 
Join Date: May 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
stopher is an unknown quantity at this point
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?
stopher is offline   Reply With Quote
Old 05-20-2009, 04:23 PM   PM User | #2
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
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..
mrhoo is offline   Reply With Quote
Old 05-20-2009, 06:44 PM   PM User | #3
stopher
New to the CF scene

 
Join Date: May 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
stopher is an unknown quantity at this point
Thanks! but... Firebug reports:
Number.prototype.valueOf called on incompatible Object
for the line return this * (Math.PI / 180);.
stopher is offline   Reply With Quote
Old 05-20-2009, 06:52 PM   PM User | #4
adios
Senior Coder

 
Join Date: Jun 2002
Posts: 1,404
Thanks: 2
Thanked 32 Times in 32 Posts
adios is on a distinguished road
See if this helps. Interesting read, regardless.
adios is offline   Reply With Quote
Old 05-20-2009, 07:33 PM   PM User | #5
stopher
New to the CF scene

 
Join Date: May 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
stopher is an unknown quantity at this point
mrhoo,
I'm wondering if the n of the Degree constructor is not getting passed to the Number constructor.
stopher is offline   Reply With Quote
Old 05-20-2009, 08:12 PM   PM User | #6
nightwolfcem
New Coder

 
Join Date: Oct 2008
Location: Turkey
Posts: 15
Thanks: 0
Thanked 1 Time in 1 Post
nightwolfcem is an unknown quantity at this point
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..
nightwolfcem is offline   Reply With Quote
Users who have thanked nightwolfcem for this post:
stopher (05-20-2009)
Old 05-20-2009, 08:56 PM   PM User | #7
stopher
New to the CF scene

 
Join Date: May 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
stopher is an unknown quantity at this point
nightwolfcem,

Woo hoo! I was struggling with .prototype, and .call and pouring over Crockford's stuff but I never though of this.
stopher is offline   Reply With Quote
Old 05-20-2009, 09:05 PM   PM User | #8
stopher
New to the CF scene

 
Join Date: May 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
stopher is an unknown quantity at this point
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?
stopher is offline   Reply With Quote
Old 05-20-2009, 11:23 PM   PM User | #9
nightwolfcem
New Coder

 
Join Date: Oct 2008
Location: Turkey
Posts: 15
Thanks: 0
Thanked 1 Time in 1 Post
nightwolfcem is an unknown quantity at this point
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..
nightwolfcem is offline   Reply With Quote
Old 05-21-2009, 06:52 AM   PM User | #10
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,462
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is online now   Reply With Quote
Old 05-21-2009, 08:46 AM   PM User | #11
nightwolfcem
New Coder

 
Join Date: Oct 2008
Location: Turkey
Posts: 15
Thanks: 0
Thanked 1 Time in 1 Post
nightwolfcem is an unknown quantity at this point
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)
nightwolfcem is offline   Reply With Quote
Reply

Bookmarks

Tags
inherit, number, oop

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:23 PM.


Advertisement
Log in to turn off these ads.