Cezary Tomczyk wrote:
> On 21/08/2017 17:47, Thomas 'PointedEars' Lahn wrote:
>> Cezary Tomczyk wrote:
>>> On 21/08/2017 16:44, Thomas 'PointedEars' Lahn wrote:
>>>> Cezary Tomczyk wrote:
>>>>> I am sure that:
>>>>>
>>>>> const par = document.createElement( 'p' );
>>>>>
>>>>> will always create HTML element. I can't see any real scenario where
>>>>> createElement method will return "something else than a reference to
>>>>> an object that has a method named “appendChild”" in that particular
>>>>> scenario.
>>>>
>>>> Why?
>>>
>>>
https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement
>>> […]
>> Certainly you can come up with something better than hearsay.
>
> Possibly there is a better explanation than just hearsay.
Possibly. Can you come up with it?
>>> In that particular case:
>>>
>>> const par = document.createElement( 'p' );
>>>
>>> this will return an HTML p element.
>>
>> It may.
>
> Well, recently you wrote:
>
> "As a rule of thumb, if the object in question always had the method and
> it always worked, I do not feature-test it."
>
> Isn't now that case?
This is not about not feature-testing the createElement() method. It is
about testing its return value before using that.
> I might be wrong, but if there are cases that may strongly require "if"
> here then I'd like to know.
You are trying to shift the burden of proof again. You have claimed that
this method can never return something unsuitable. So how do *you* support
that claim *other* than with hearsay from MDN (I called it “hearsay” because
it is a wiki and there is no other reference attached to your claim)?
>>> I would say and even, if it'll occur then calling "appendChild" later
>>> will throw a DOM exception which I think it's good because it helps to
>>> catch the error on an early stage.
>> An exception, but not a “DOM exception” will be thrown, if the object
>> does not have the method. Or execution will simply stop with an error.
>> Who knows. createElement() is a host object’s method.
>
> Right, an exception.
So, what if no exception is being thrown by a method, would you not want to
make sure as best as you can that the return value of a method is actually a
reference to an object, before you attempt to access its properties?
> createElement() is a host object’s method, but it was always available
This is simply not true. Maybe you are too young to know it:
Before the W3C DOM, which became a W3C Recommendation in 1998 CE (and was
quickly adopted through Mozilla 1.0 in 2000), there have been element
objects that did not have that method because it was introduced with the
aforementioned API.
In what was called “Dynamic HTML” (DHTML) at the time, in Netscape Navigator
(and later, Communicator) 4.x, there were layer objects for `LAYER` (and
IIRC, `DIV`) elements whose content you could replace by calling their
write() method; and in Internet Explorer and other MSHTML-based user agents
there were element objects which had only the “innerHTML” and “outerHTML”
properties, and several other methods (like insertAdjacentHTML() recently
mentioned by Martin) for this instead.
> and unless it's not modified it has been always working quite good.
Based on what *data*?