On Oct 16, 11:43 am, "Jay" <j...@mail.comwrote:
If I create a variable outside of a function like:
var futureDate = (new Date() + 10000);
and I want to use that variables value inside a function, how can I make
sure each time the function runs the futureDate variable contains its
original value?
Do you mean you want to make futureDate "const" like in C? If so you
can't do that in JavaScript. If you want to ensure that the value of
futureDate doesn't change then you just don't change it.
There are ways you can reduce the scope of the futureDate variable so
less of you code can modify futureDate rather than having futureDate a
global variable. Closures are one way...
var getFutureDate = (function() {
var fd = (new Date() + 10000);
return function() {
return fd;
};
})();
Now every time you call getFutureDate() it will return the same value
and no code can change that "hidden" fd variable; however, some code
could redefine getFutureDate which would screw up everything.
Peter