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

Abbreviating a statement

P: n/a
Is there anything incorrect or risky by writing

var i = edit_array[5] - 1;
document.form1.cbt[i].checked=true;

as

document.form1.cbt[edit_array[5]-1].checked=true;

--
Ed Jay (remove 'M' to reply by email)

Win the War Against Breast Cancer.
Knowing the facts could save your life.
http://www.breastthermography.info
Sep 4 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Ed Jay wrote:
Is there anything incorrect or risky by writing

var i = edit_array[5] - 1;
document.form1.cbt[i].checked=true;

as

document.form1.cbt[edit_array[5]-1].checked=true;
If edit_array[5] <= 0, edit_array[5] % 1 != 0, or isNaN(edit_array[5]) ==
true, there is (probably) no document.form1.cbt[edit_array[5] - 1] and the
`checked' property lookup would throw a TypeError exception. With the first
approach you can test for the value of `i' before you use it, instead. In
any case you would want to test whether `document.form1.cbt[i]' referred to
an object before you attempted to access its properties.

Besides,

document.forms["form1"].elements["cbt"][edit_array[5] - 1]

is the standards-compliant and backwards-compatible way to refer to form
control objects.

<http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html>
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Sep 4 '08 #2

P: n/a
Thomas 'PointedEars' Lahn wrote:
>Ed Jay wrote:
>Is there anything incorrect or risky by writing

var i = edit_array[5] - 1;
document.form1.cbt[i].checked=true;

as

document.form1.cbt[edit_array[5]-1].checked=true;

If edit_array[5] <= 0, edit_array[5] % 1 != 0, or isNaN(edit_array[5]) ==
true, there is (probably) no document.form1.cbt[edit_array[5] - 1] and the
`checked' property lookup would throw a TypeError exception. With the first
approach you can test for the value of `i' before you use it, instead. In
any case you would want to test whether `document.form1.cbt[i]' referred to
an object before you attempted to access its properties.
edit_array[n] (and relevant form elements) always exist, and are thoroughly
tested for existence before the above..
>
Besides,

document.forms["form1"].elements["cbt"][edit_array[5] - 1]

is the standards-compliant and backwards-compatible way to refer to form
control objects.

<http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html>
How important is this compliance issue? I have at least 100's of such
statements contained in some 15 scripts I would have to change.

Thank you for your input.

--
Ed Jay (remove 'M' to reply by email)

Win the War Against Breast Cancer.
Knowing the facts could save your life.
http://www.breastthermography.info
Sep 4 '08 #3

P: n/a
Ed Jay wrote:
Thomas 'PointedEars' Lahn wrote:
>Ed Jay wrote:
>>Is there anything incorrect or risky by writing

var i = edit_array[5] - 1;
document.form1.cbt[i].checked=true;

as

document.form1.cbt[edit_array[5]-1].checked=true;
If edit_array[5] <= 0, edit_array[5] % 1 != 0, or isNaN(edit_array[5])
== true, there is (probably) no document.form1.cbt[edit_array[5] - 1]
and the `checked' property lookup would throw a TypeError exception.
[...]

edit_array[n] (and relevant form elements) always exist, and are
thoroughly tested for existence before the above..
Then, and only then, it is a merely matter of taste and maintainability vs.
runtime efficiency.
>Besides,

document.forms["form1"].elements["cbt"][edit_array[5] - 1]

is the standards-compliant and backwards-compatible way to refer to
form control objects.

<http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html>

How important is this compliance issue?
As important as you think adhering to industry standards is.
I have at least 100's of such statements contained in some 15 scripts I
would have to change.
Then you have probably done something very inefficient in the past, and it
would be about time that you refactored your code, even though advanced
search-and-replace could provide a quick fix. If you provided a sample of
it, one could make recommendations to that end.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Sep 4 '08 #4

P: n/a
Thomas 'PointedEars' Lahn wrote:
>Ed Jay wrote:
>Thomas 'PointedEars' Lahn wrote:
>>Ed Jay wrote:
Is there anything incorrect or risky by writing

var i = edit_array[5] - 1;
document.form1.cbt[i].checked=true;

as

document.form1.cbt[edit_array[5]-1].checked=true;
If edit_array[5] <= 0, edit_array[5] % 1 != 0, or isNaN(edit_array[5])
== true, there is (probably) no document.form1.cbt[edit_array[5] - 1]
and the `checked' property lookup would throw a TypeError exception.
[...]

edit_array[n] (and relevant form elements) always exist, and are
thoroughly tested for existence before the above..

Then, and only then, it is a merely matter of taste and maintainability vs.
runtime efficiency.
>>Besides,

document.forms["form1"].elements["cbt"][edit_array[5] - 1]

is the standards-compliant and backwards-compatible way to refer to
form control objects.

<http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html>

How important is this compliance issue?

As important as you think adhering to industry standards is.
>I have at least 100's of such statements contained in some 15 scripts I
would have to change.

Then you have probably done something very inefficient in the past,
Clearly. I'm wondering where did I learn to code the wrong way.
and it
would be about time that you refactored your code, even though advanced
search-and-replace could provide a quick fix. If you provided a sample of
it, one could make recommendations to that end.
A good sample is the format I posted. I'm quite interested in complying with
industry standards, so it appears I have some work to do. Amending
document.form1.variableName.whatever to comply looks to be fairly simple
search/replace for the most part.

--
Ed Jay (remove 'M' to reply by email)

Win the War Against Breast Cancer.
Knowing the facts could save your life.
http://www.breastthermography.info
Sep 4 '08 #5

P: n/a
Ed Jay wrote:
Thomas 'PointedEars' Lahn wrote:
....
>Then, and only then, it is a merely matter of taste and
maintainability vs. runtime efficiency.
Efficiency and performance is often mentioned in situations
like this. One could test the existence of an object typically
100 000 - one million times for one page load
before the extra delay would be annoying.

And writing 100 000 lines of existence tests is a lot of
work for the programmer, I guess.

As a user I could be happy to wait 0.000 01 seconds longer, if
I get a better program.

Sep 4 '08 #6

P: n/a
Ed Jay wrote:
Thomas 'PointedEars' Lahn wrote:
>Ed Jay wrote:
>>I have at least 100's of such statements contained in some 15 scripts
I would have to change.
Then you have probably done something very inefficient in the past,

Clearly. I'm wondering where did I learn to code the wrong way.
>and it would be about time that you refactored your code, even though
advanced search-and-replace could provide a quick fix. If you provided
a sample of it, one could make recommendations to that end.

A good sample is the format I posted.
Not for this, it isn't. What I meant is, for example: Instead of writing

var x = a.b.c.d.e.f;
var y = a.b.c.d.e.g;
var z = a.b.c.f.e.h;

you should be writing

var o = a.b.c.d.e;
var x = o.f;
var y = o.g;
var z = o.h;

The same applies for property accesses that use the bracket form.
I'm quite interested in complying with industry standards, so it appears
I have some work to do. Amending document.form1.variableName.whatever to
comply looks to be fairly simple search/replace for the most part.
Good luck, then.

Please trim your quotes to the minimum required to retain the context.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Sep 4 '08 #7

P: n/a
In comp.lang.javascript message <0a0vb4huf7f2nb9kos9erb3uel1m4lr4qa@4ax.
com>, Wed, 3 Sep 2008 23:35:30, Ed Jay <ed***@aes-intl.composted:
>Is there anything incorrect or risky by writing

var i = edit_array[5] - 1;
document.form1.cbt[i].checked=true;

as

document.form1.cbt[edit_array[5]-1].checked=true;
Evidently the Great Ranter has not understood, or has wantonly ignored,
the exact point of your sufficiently well-phrased question, to which the
answer is "no, unless there are further lines using variable 'i' which
would also get expanded.". But it might be of negative benefit if 'i'
were a variable with an explanatory name serving as comment. And
there's some merit in avoiding 'i' in disseminated material, as it might
in some fonts be misread.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk DOS 3.3 6.20 ; WinXP.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm- also batprogs.htm.
Sep 4 '08 #8

P: n/a
Dr J R Stockton wrote:
>In comp.lang.javascript message <0a0vb4huf7f2nb9kos9erb3uel1m4lr4qa@4ax.
com>, Wed, 3 Sep 2008 23:35:30, Ed Jay <ed***@aes-intl.composted:
>>Is there anything incorrect or risky by writing

var i = edit_array[5] - 1;
document.form1.cbt[i].checked=true;

as

document.form1.cbt[edit_array[5]-1].checked=true;

Evidently the Great Ranter has not understood, or has wantonly ignored,
the exact point of your sufficiently well-phrased question, to which the
answer is "no, unless there are further lines using variable 'i' which
would also get expanded.". But it might be of negative benefit if 'i'
were a variable with an explanatory name serving as comment. And
there's some merit in avoiding 'i' in disseminated material, as it might
in some fonts be misread.
Thanks, John. :-)

--
Ed Jay (remove 'M' to reply by email)

Win the War Against Breast Cancer.
Knowing the facts could save your life.
http://www.breastthermography.info
Sep 4 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.