To llustrate the problem, type the following in the firebug console
and run it:

   x = "100"
   y = "200"
   x += 1*y
   console.log("x = "+x);
   x = "100"
   y = "200"
   x = x + 1*y
   console.log("x expanded = "+x);

You will see the string concatenation and result:

   x= "100200"
   x expanded = "100200"

The JS core documentation says that the addition + operator has a left-
to-right association and it also has a lower precedence over the
multiplication.

So 1*y should evaluated first to produce a numeric value, so we have:

    string = string + postive_number*string

which becomes:

    string = string + number

That would be a string concatenation as shown in the console above.

However using the unary negation resolve this:

   x = "100"
   y = "200"
   x -= -1*y
   console.log("x = "+x);

and you get x = 300.

Understanding this, this is how it expands:

    x = x - -1*y

or

    string = string - negative_number * string

which reduces to

    string = string - number

so I guess, there is no bug here because this is not expected to
concatenate the parts here.  A negative operator natually has the
presumption of arithmetic.

No bug!

Just one of those odd things one needs to keep in mind when using the
assigment += operator.

Never mind!

--
HLS


On Aug 23, 9:39 am, Pops <[EMAIL PROTECTED]> wrote:
> I posted this as a subtopic in another message,
>
> http://groups.google.com/group/jquery-en/browse_thread/thread/f54857c...
>
> but I now see that probably is a separate JS issue that the purist
> here might appreciate.
>
> Not to long ago, I came across JS behavior and just noted it down.
> The issue in the reference link above reminded me of this.
>
> This code is basically part of  First, Prev, Next, Last, table
> navigator for our ad hoc report/table generator. It is pre-jQuery
> work.  So here I am using the $() function to get the element.
>
> function $(v) { return(document.getElementById(v)); }
> function gotoPrevPage()
> {
>   $("start").value -= 1*$("rows").value;    // <- WORKS AS EXPECTED
>
> }
>
> function gotoNextPageBUG()
> {
>   $("start").value += 1*$("rows").value;      // <- BUG!!
>
> }
>
> function gotoNextPageFIX()
> {
>   $("start").value -= -1*$("rows").value;   // <- FIX!!
>
> }
>
> I was multiplying by 1 to help JS type cast the operation to tell it
> that it adding or substracting a number rather than a string.   If you
> remove the multiplier, JS will naturally concatentate strings.
>
> But that didn't work for the gotoNextPage() function using the
> inclusive +=  operation.  It worked fine with -= operation in
> gotoPrevPage().
>
> So I used the reverse logic to multiply by -1 instead with -= and it
> worked.
>
> To me,  that looks look like a JS type casting bug or inclusive
> addition bug?
>
> What say you?
>
> --
> HLS

Reply via email to