D
David Mark
This may be true because `document.body' could be considered to refer
to the same object on every call. However, that does not need to be so..
The method may be reused in another global context, for example.
Add me.
Not testing the return value would be at least unwise. Per W3C DOM Level 2
Core the createElement() method may throw a DOMException
(INVALID_CHARACTER_ERR) "if the specified name contains an illegal
character"; implementations vary, of course. For example, it is reasonable
to assume that if there is any error creating the object (that is, at least
reserving heap memory for it), the method call will fail in some way. So
the return value may as well not be a reference to a host object. However,
we can safely assume (based on empirical data) that if it is not a reference
to a host object it is a false-value, and (employing simple logic) that if
it is a reference to a host object that the result of the expression is a
true-value, and the property access does not throw an exception (else
`wrapper.style' aso. would not work, too).
I used to test the return of createElement. I just don't believe
there are agents that return null. Could be a bad assumption.
The wise answer is "Yes and yes." `elAbs' is used as the base object of a
property access below, so it needs to be tested or the property access could
throw a ReferenceError (or worse). And since it does not make sense to
continue if creation of the object failed, that object reference should be
tested as early as possible, which would be exactly here.
Same thing.
var elRel = document.createElement('div');
// should we perform the same check here again?
Yes.
[...]
var elStatic = document.createElement('div');
// what about this one?
Yes.
Whatever.
The wise answer is "Definitely yes."; everything else would be a type II
object inference, known to be error-prone. Never rely on that if an object
implements one method specified for an API or is created by another
implemented method specified for the same API, it would support a third
method specified for that API.
Bah. A DIV is a DIV. You are saying that a TABLE (or whatever) won't
support appendChild, while DIV's will? I doubt it.
Correct, that test should happen directly after the assignment to `el'.
Because currently we continue with body.insertBefore() despite the
possibility of `el' being not an object reference (which does not make
sense), and `el' is used as an argument to a host method without being
tested first.
You can really beat a dead horse. I'm still not testing the result of
createElement (at least not in most contexts.)
Logic suggests that it SHOULD NOT be tested this way because the result of
the `typeof' operation may not indicate the type of the result of the plain
expression.
What on earth are you talking about. If el.offsetTop is not a number,
you are out of luck.
That test is fine as it is . It is already a one-time test for property of
the host object previously created, because that object is a different one
on every call.
So one-off feature tests are not a good idea? I disagree. You can't
test every property every time (and it's silly anyway.)
Please trim your quotes to the necessary minimum required to retain the
context of your replies.
No.