2

I'm studying some JS code and compared to other languages it's not making any sense to me. First, there's a date object that's declared and that I understand,

$ns.constant = {
    date: {}, /* Input date */
};

Then there's functions like this one below where a date is passed to a function. Here's what I don't understand. In other languages, when you pass a variable to a function, that function creates a new variable that's local to the function and the function acts upon that local variable.

This function returns date.julian. But it also adds other dates like date.j2000 and date.1950. In my understanding of other languages, those aren't being returned because they're the date object local to the function. It would seem to me that in order to put those values into the date constant you'd have to return the entire date object the function created and not just date.julian. Or, is js just seeing that date being passed and somehow knows that it's the date constant?

Is this function modifying the date constant, an object that's local to the function or what?

Thanks

$ns.julian.calc = function (date) {
    var centuries;
    var year;
    var month;
    var b = 0;
    var c;
    var e;

    year = date.year + 4800;
    if (date.year < 0) {
        year += 1;
    }

    month = date.month;
    if (month <= 2) {
        month += 12;
        year -= 1;
    }
    e = Math.floor ((306 * (month + 1)) / 10);

    centuries = Math.floor (year / 100);

    if (date.year <= 1582) {
        if (date.year == 1582) {
            if (date.month < 10) {
                b = -38;
            }
            if (date.month > 10 || date.day >= 15) {
                b = Math.floor ((centuries / 4) - centuries);
            }
        }
        else { b = -38; } 
    } else {
        b = Math.floor ((centuries / 4) - centuries);
    }
    c = Math.floor ((36525 * year) / 100);

    date.julianDate = b + c + e + date.day - 32167.5;

    date.julianTime = (3600.0 * date.hours + 60.0 * date.minutes + date.seconds) / 86400.0;

    date.julian = date.julianDate + date.julianTime;

    date.j2000 = 2000.0 + (date.julian - $const.j2000) / 365.25;
    date.b1950 = 1950.0 + (date.julian - $const.b1950) / 365.25;
    date.j1900 = 1900.0 + (date.julian - $const.j1900) / 365.25;

    return date.julian;
};
1
  • Objects in JavaScript are passed into functions "by reference". It's like a link to the actual object instead of a local copy of it. This is for performance reasons. Check out javascript.info or any of these other sites for details. Commented Nov 1, 2021 at 13:55

1 Answer 1

1

It appears that you're passing a date "object" to your function and then adding things to it, which will make the original date passed to that function update as well, not just local to the function.

In all languages, not just javascript, objects and heap references are passed by reference, while stack variables are passed by value (a copy local to the function). An object, in any language, will be passed by reference and whatever you change on that object will also make the original object change.

That's because the "date" that you're passing is a pointer or reference to a heap location in memory where you're updating things, so when you later, outside of that function, use "date" again it's still pointing to the same heap address where you updated things. Changing the values on a heap reference will reflect in anything that's pointing to the same reference.

You might also be confused with how immutability works since you declared "date" as a constant, but you assigned that date an object. What that means is that the reference to that heap location (date) can't change, so you couldn't i.e. do date = {} since you can't reassign a new address to that pointer, however the values on that object can still change, they are not immutable, so you can do something like date.example = 'example'. I'm not aware of a data structure in JS that's recursively immutable unless you use something like https://immutable-js.com/.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! That makes sense. What really confused me was first, passing a date variable and then returning just the date.julian. If there were no return value or just the date was returned, I would have pretty much known it was the constant being changed. The passed date and return value made me stop and think, wait, is this a function local date or the constant? For the sake of my own sanity in PHP I wouldn't have passed the date and just used something like global $date.
You don't need any return in that function, you can remove the return and it will still work just the same. The date outside will still be updated. As for PHP, it works exactly the same, if you passed any DateTime or class object it would work just the same. And PHP and JS are exactly the same in regards to using the date without passing it to the function, you can also declare a date outside the function and just use it inside, you didn't need to pass that date to the function.
That was another part of the confusion, passing that date. He's also doing something in the JS code I've never seen done in any language and I'm still not sure if it's brilliance or insanity, but he's using one giant object ($ns.constant) to hold all of the global variables. In PHP I'd expect to see something like $this->constant->date. Instead, he's just using date. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.