Re: Yet Another Newbie Question
- From: Thomas 'PointedEars' Lahn <PointedEars@xxxxxx>
- Date: Mon, 03 Dec 2007 02:15:11 +0100
Marten Kemp wrote:
The final project is creating a _very_ simple
e-commerce website. It's outside the scope of the
class but I'd like to include a rudimentary
shopping cart feature - just some way to keep track
of items added and then list them on the checkout
page.
It should be self-evident that an e-commerce Web site that depends on
client-side script support is not a viable option as that feature can
be disabled, or not even present. Client-side scripting can provide
additional features where supported (like computing totals on the fly),
but any input has to be double-checked on the server, of course.
[...]
I'm going to include what I have so far and would
any kind soul give me a pointer or two?
// JavaScript Document
function e_initialize() {
alert('Initialize script called.');
e_cart_numz = new Array(100);
e_cart_descr = new Array(100);
e_cart_price = new Array(100);
`e_cart_numz', `e_cart_descr', and `e_cart_price' should be declared if they
are to be variables. They should be declared globally if they are to be
used globally, as indicated by the code below. But then an initialization
method may be unnecessary as that could take place in the global execution
context as well.
Arrays in ECMAScript implementations are of dynamic length by nature. It
does not make sense to initialize them with a length this way. The best it
can do, as compared to an initialization with an empty argument list, is to
set the `length' property; the worst it can do is to initialize it with one
element, the number value 100.
for (index = 0; index < 100; index++)
`index' has to be declared if it is to be a (local) variable. And since
order does not matter here, it is more efficient to iterate vice-versa:
for (var index = 100; index--;)
{ e_cart_numz[index] = -1;
e_cart_descr[index] = "";
e_cart_price[index] = 0; }
But again, that does not make sense.
}
function e_add_cart (number, description, price) {
var e_cart_numz;
var e_cart_descr;
var e_cart_price;
These variable declarations will "overshadow" the globally defined
identifiers, therefore ...
alert('e_add_cart called.');
alert('number="'+number+'"');
alert('description="'+description+'"');
alert('price="'+price+'"');
var count = e_cart_numz.push(number);
count = e_cart_descr.push(description);
count = e_cart_price.push(price);
.... these last three calls will cause a runtime error each, as `undefined'
has no properties.
Even if the declarations above were omitted, that does not make sense as
well. Although Array.prototype.push() is a method that returns a value
different from `undefined', you are not required to evaluate that value,
much less to assign it to a variable or to overwrite the assigned value
in the next line.
return count; }
I don't see you evaluating the return value.
function e_del_cart (index) {
var e_cart_numz;
var e_cart_descr;
var e_cart_price;
See above. Apparently you have gotten the misconception from the scoping
procedures of other programming languages that you need to declare the usage
of global variables in the local context in ECMAScript implementations
before you can use them. That is not the case; globally available
identifiers are implicitly available in local context instead.
alert('e_del_cart called.');
e_cart_numz[index] = -1 ;
e_cart_descr[index] = "" ;
e_cart_price[index] = 0 ;
delete e_cart_numz[index];
delete e_cart_descr[index];
delete e_cart_price[index];
would most certainly suffice. It would not reset the original values, but
it would remove the corresponding array elements, which is why property read
access with the corresponding indexes would yield `undefined'.
But instead of three separate arrays one should employ only one array to
contain references to extended Object objects as its elements.
var cart = [
{
descr: 'foo',
numz: 2,
price: 23.00
},
{
descr: 'bar',
numz: 3,
price: 0.42
},
];
return ; }
That is unnecessary. Methods that do not explicitly return a value, return
`undefined', the sole value of the Undefined type.
You want to read the FAQ, especially on how to post properly to and in
Usenet, and where to find reference material for software development:
http://jibbering.com/faq/
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
.
- Follow-Ups:
- Re: Yet Another Newbie Question
- From: Evertjan.
- Re: Yet Another Newbie Question
- From: Randy Webb
- Re: Yet Another Newbie Question
- From: Marten Kemp
- Re: Yet Another Newbie Question
- References:
- Yet Another Newbie Question
- From: Marten Kemp
- Yet Another Newbie Question
- Prev by Date: Re: HIDE/SHOW Layer HELP!
- Next by Date: Re: ajax load image
- Previous by thread: Yet Another Newbie Question
- Next by thread: Re: Yet Another Newbie Question
- Index(es):
Relevant Pages
|