Re: Cookie problem! Getting error when passing the expire date.
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: Cookie problem! Getting error when passing the expire date.

From: Lasse Reichstein Nielsen <lrn@hotpop.com>
Date: Sun Apr 30 2006 - 11:58:00 CEST

"Evertjan." <exjxw.hannivoort@interxnl.net> writes:

> You are right.
>
> Because a string, like a zero, equals false, when |-ing.

More precisely, since "|" is a number operator, not a logical
operator, a non-numeral string converts to NaN.

(For logical comparison, i.e., conversion to boolean, only
the empty string becomes false).

There are a lot of implicit type conversions in
 x | 1
where "x" holds a string. More explicitly (using the internal names
for conversion and showing only the ones that makes a difference) it
corresponds to:

 toInt32(toNumber(x)) | toInt32(1)

If the string in "x" is not a numeral (as per the definition of
"toNumber"), "toNumber(x)" yields "NaN". Then "toInt32(NaN)"
yields "+0" and "0 | 1" gives 1.

> x='0'
> y=x|1
> alert(y); // returns 1,
> //"should" return 0, x being convertable to a number.

And
 x='24'
 y = x|1;
 alert(y); // alerts 25

"Bitwise or" is not a logical guard in the way "logical or" is.
This zero-problem would still be there for || though:
 Number("0") || 1
yields 1 as well, since 0 converts to false.

It really should be something like:
  
function toNumberWithDefault(x,def) {
  var n = Number(x);
  return isNaN(n) ? def : n;
}

/L

-- 
Lasse Reichstein Nielsen  -  lrn@hotpop.com
 DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
  'Faith without judgement merely degrades the spirit divine.'
Received on Mon May 1 05:27:49 2006