By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,685 Members | 1,896 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,685 IT Pros & Developers. It's quick & easy.

Variable question

P: n/a
Jay
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?

Oct 16 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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?

Oct 16 '07 #2

P: n/a
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

Oct 16 '07 #3

P: n/a
Jay
Thanks, got it working.
Oct 16 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.