...

View Full Version : Problem inheriting from core javascript



stopher
05-20-2009, 03:03 PM
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:


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?

mrhoo
05-20-2009, 04:23 PM
I'd just add a method to the Number prototype-

Number.prototype.degtoRad=function(){
return this*(Math.PI/180)
}

stopher
05-20-2009, 06:44 PM
Thanks! but... Firebug reports:
Number.prototype.valueOf called on incompatible Object
for the line return this * (Math.PI / 180);.

adios
05-20-2009, 06:52 PM
See if this (http://javascript.crockford.com/inheritance.html) helps. Interesting read, regardless.

stopher
05-20-2009, 07:33 PM
mrhoo,
I'm wondering if the n of the Degree constructor is not getting passed to the Number constructor.

nightwolfcem
05-20-2009, 08:12 PM
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..

stopher
05-20-2009, 08:56 PM
nightwolfcem,

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

stopher
05-20-2009, 09:05 PM
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?

nightwolfcem
05-20-2009, 11:23 PM
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");

rnd me
05-21-2009, 06:52 AM
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...

nightwolfcem
05-21-2009, 08:46 AM
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)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum