Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8339015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:46:05+00:00 2026-06-09T04:46:05+00:00

I am trying to get my object’s method to work, but am missing something

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:46:06+00:00Added an answer on June 9, 2026 at 4:46 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

iam trying to get all object's xpath's from loaded page via selenium something similar
I'm trying to get an object back from an NHibernate query. My method is
I am trying to get a SharedPreferences object from the PreferenceManager but don't want
I am trying to get an object using Reflection, and then launch a method
I'm using Rails 2.3.2, and trying to get a nested object form to work
I'm trying to get a binding to work on a child object of a
I am trying to get a MethodInfo object for the method: Any<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>)
I'm trying to get the document object of an iframe, but none of the
I am trying to get a calendar object set to GMT, but the getTime()
I am trying to get registry key values but i am facing issues. Following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.