Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #9284 > unrolled thread
| Started by | Gene Wirchenko <genew@ocis.net> |
|---|---|
| First post | 2011-12-15 14:42 -0800 |
| Last post | 2011-12-16 22:45 +0000 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.javascript
Defining Constants Gene Wirchenko <genew@ocis.net> - 2011-12-15 14:42 -0800
Re: Defining Constants Stefan Weiss <krewecherl@gmail.com> - 2011-12-16 01:04 +0100
Re: Defining Constants Dr J R Stockton <reply1150@merlyn.demon.co.uk> - 2011-12-16 22:45 +0000
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2011-12-15 14:42 -0800 |
| Subject | Defining Constants |
| Message-ID | <c8nke7t4hcpkapltpsntad1d55k7le2q8u@4ax.com> |
Dear JavaScripters:
I want to include some constants for use with my date type. This
is related to my question about toString() parameters.
Should I do
1) Defined in Object
function JustDate()
{
...
this.JDFmtISO8601=1;
this.JDFmtIMA=2;
...
or
2) Defined Outside Object
JDFmtISO8601=1;
JDFmtIMA=2;
function JustDate()
{
...
Does it really matter?
Sincerely,
Gene Wirchenko
[toc] | [next] | [standalone]
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Date | 2011-12-16 01:04 +0100 |
| Message-ID | <drGdnaI6NILPFnfTnZ2dnUVZ8g2dnZ2d@giganews.com> |
| In reply to | #9284 |
On 2011-12-15 23:42, Gene Wirchenko wrote:
> I want to include some constants for use with my date type. This
> is related to my question about toString() parameters.
>
> Should I do
>
> 1) Defined in Object
>
> function JustDate()
> {
> ...
> this.JDFmtISO8601=1;
> this.JDFmtIMA=2;
> ...
I would advise against this. It's semantically untidy - the constants
aren't really properties of each individual JustDate object, they are
shared by all of them. Additionally, the constants are created and
initialized every time you create a JustDate instance. It would also be
theoretically possible to have two JustDate objects with different
values for the same "constant". If you have to do this, at least put
them in JustDate.prototype.
> 2) Defined Outside Object
>
> JDFmtISO8601=1;
> JDFmtIMA=2;
>
> function JustDate()
> {
These two look like unintentional globals. If want global visibility,
you should either use 'var' at the global scope, or explicitly set them
as properties of the global object (or the window object, if you want to
limit yourself to web browsers).
Personally, I would add them to the JustDate constructor directly. This
way, they get defined close to where they are actually read, and you
don't mess up the global namespace:
function JustDate() {
... implementation ...
}
JustDate.fmtISO = 1;
JustDate.fmtIMA = 2;
> Does it really matter?
Only in the sense that clean code and good design matter. All of these
will work in a pinch.
By the way, are you aware that there is a 'const' keyword? It's not yet
part of an official ECMAScript standard (it's scheduled for ES6, IIRC),
but several browsers already support it. Depending on where you're
planning to deploy your JustDate, 'const' might be available. The
obvious advantage is that constants defined with 'const' are actually
constant (unchangeable). You can also get unchangeable properties with
ES5's Object.defineProperty() method.
- stefan
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1150@merlyn.demon.co.uk> |
|---|---|
| Date | 2011-12-16 22:45 +0000 |
| Message-ID | <Jdl9urMGo86OFwTu@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #9284 |
In comp.lang.javascript message <c8nke7t4hcpkapltpsntad1d55k7le2q8u@4ax.
com>, Thu, 15 Dec 2011 14:42:00, Gene Wirchenko <genew@ocis.net> posted:
>
>1) Defined in Object
>
> function JustDate()
> {
>...
> this.JDFmtISO8601=1;
> this.JDFmtIMA=2;
>...
Avoid "JD" in this context; it already has an established meaning in
connection with dates.
Consider also
var GWC = { JDFmtISO8601 : 1 , JDFmtIMA : 2 , ... }
--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web