Reputation: 13
I am trying to get my object's method to work, but am missing something - probably very obvious!!
I found the following topic: How do you create a method for a custom object in JavaScript? but no matter what I try, my method just returns the function code as a literal string.
I also tried looking at the object literal JavaScript - Advantages of object literal but as a newcomer to JS, I couldn't see how to pass initial parameters to the object!
So, context....
I am creating a web page that does various astronomical calculations based on date, time and location. I have a set of inputs for year, month, day, hour, min, sec, timezone, latitude and longitude. I am using this as a learning experience for JS, as well as understanding the calculations!
I am creating a global date object to hold the date elements and I wanted to create a method that would calculate the Julian Day Number.
I have created a fiddle with the code: http://jsfiddle.net/AstroDaz/gKQ5f/5/
function DATE_OBJ (iyear,imonth,iday,idirty,ijd) {
this.Year = iyear;
this.Month = imonth;
this.Day = iday;
this.Dirty = idirty;
this.JD = DateJD;
}
function DateJD() {
var Y = objDate.Year;
var M = objDate.Month;
var D = objDate.Day;
if (M < 3) {
--Y;
M +=12;
}
if (objDate.Year > 1582) {
var A = parseInt(Y / 100, 10);
var B = 2 - A + parseInt(A / 4,10);
} else {
if (objDate.Year = 1582 && M > 10) {
var A = parseInt(Y / 100, 10);
var B = 2 - A + parseInt(A / 4,10);
} else {
if (objDate.Year = 1582 && M = 10 && D >=15) {
var A = parseInt(Y / 100, 10);
var B = 2 - A + parseInt(A / 4,10);
} else var B = 0;
}
}
if (Y < 0) var C = parseInt((365.25 * Y) - 0.75,10);
else var C = parseInt(365.25 * Y,10);
var E = parseInt(30.6001 * (M + 1),10);
this.JD = B + C + E + objDate.Day + 1720994.5;
}
var d = new Date();
var objDate = new DATE_OBJ (d.getFullYear(),d.getMonth(),d.getDate(),false,0);
document.write(objDate.JD);
As you'll see, the result is that the JD property of the object is assigned as the literal string of the method code, not the result of the calculation!
Incidentally, if I create the DateJD as a straight function and call it, it works and returns the correct result.
Thanks for any help! Daz
Upvotes: 1
Views: 218
Reputation: 14492
With this.JD = DateJD
you're not assigning the return value but a callable function. So your calculations are never executed but just assigned to the member variable.
If you change the last line of DateJD
from
this.JD = B + C + E + objDate.Day + 1720994.5;
to
return B + C + E + objDate.Day + 1720994.5;
and then call it via
document.write(objDate.JD());
It will return a number: http://jsfiddle.net/gKQ5f/6/
If you execute your function it in your 'constructor', then there's no existing objDate
object which can be used and you will get something like
Uncaught TypeError: Cannot read property 'Year' of undefined
Btw, you might want to change these conditions:
if (objDate.Year = 1582 && M > 10) {
This assigns objDate.Year to 1582 instead of checking if it equals.
Take objDate.Year == 1582
instead.
Upvotes: 1