"Orphan" pages on a website

  • Thread starter Thread starter Trevor L.
  • Start date Start date
Trevor said:
I understood that, but the other repsonses have confused me

The rest of the discussion has been about the nuances of coercion
(http://en.wikipedia.org/wiki/Type_conversion#Implicit_type_conversion) in
JScript.

JScript will coerce several values to false. According to the MS description
of the Boolean Object, these are: 0 (numeric zero), null, NaN or "" (empty
string):

boolObj = new Boolean([boolValue])

boolValue
Optional. The initial Boolean value for the new object.
If Boolvalue is omitted, or is false, 0, null, NaN, or
an empty string, the initial value of the Boolean object
is false. Otherwise, the initial value is true.

http://msdn.microsoft.com/library/en-us/script56/html/d67748f2-7bf5-4889-8269-e777616cc5f0.asp

What this does not explicitly state is the fact that the special value
[undefined] also coerces to false (it is actually implicit in omission of
boolValue). You can illustrate this point for yourself:

var x
alert(x) // undefined
alert(new Boolean(x)) // false

It is worth noting what else this definition lacks. Consider:

alert(new Boolean(false)) // false
alert(new Boolean(new Boolean(false))) // true

Why is the second one true? HINT:

typeof false == "boolean"
typeof new Boolean(false) == "object"

So the description of coercion to boolean ought to state that certain TYPES
have values that coerce to false:

object : null
number : 0 or NaN
string : ""
boolean : false
undefined : undefined

Here's where it gets fun. The following are true:

(0 == 0)
("0" == 0)
(new Boolean(0).valueOf() == new Boolean(0).valueOf())

While this is false:


(new Boolean("0").valueOf() == new Boolean(0).valueOf())

Think about that. The equality operator coerces "0" and 0 into the same type
for comparision**, so they "look" equal. But the Boolean Object coerces them
to opposite values.


**If you want to know the type, ask yourself what you will get if you try
this: alert(0.0=="0"). Then test it.


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
Daev,

All I can say is Wow. I'll read, learn and inwardly digest :-))

Just as well I am an ex-programmer or I would have got thoroughly lost
 
Trevor said:
All I can say is Wow. I'll read, learn and inwardly digest :-))

I got a little off-track and never came back to one point. Allow me to
continue.

Whenever you use the "new" operator, you get something of type "object". In
other words,

typeof new String() == "object"
typeof new Number() == "object"
...
typeof new Object() == "object"

Why is this important? Recall that only one "object" coerces to false: null.
This leaves the following dilemma:

Item Coerces to
=============== ==============
0 false
new Number(0) true
"" false
new String("") true
0/0 false
new Boolean(0/0) true

Now that you know this, you will understand why we occasionally use terms
like "string value" and "String Object" instead of just "string". A string
value is something with type "string". As you can see, it is not identical
to a String Object.

var a = "abc", b = new String("abc")
alert(a == b) // true
alert(typeof a == typeof b) // false
alert(a == b.valueOf()) // true

Now the doozy:

alert(a.valueOf() == b.valueOf()) // true

Because .valueOf() is a method of all the "object" types, the string value
"abc" has to be coerced into a String Object before that method can be
called.

See how much fun JScript is?



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
Back
Top