Skip to content

Commit 79c71ef

Browse files
committed
Add some text for factory methods vs constructors (resolves #44)
1 parent 0e3164c commit 79c71ef

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

index.bs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ spec:css-cascade-5; type:dfn; text:inherit
2323
spec:dom
2424
type:dfn; text:aborted flag
2525
type:method; for:Document; text:getElementsByTagName(qualifiedName)
26+
type:method; for:Document; text:createElement(qualifiedName)
2627
spec:html; type:dfn; for:/
2728
text:focus update steps
2829
text:task queue
@@ -31,6 +32,8 @@ spec:html; type:element-attr; for:a; text:href
3132
spec:html; type:event; text:resize
3233
spec:selectors-4; type:selector; text::hover
3334
spec:webidl; type:dfn; text:namespace
35+
spec:uievents
36+
type:method; for:Document; text:initMouseEvent()
3437
</pre>
3538
<!-- Some of these 'anchors' entries are really routing around spec bugs.
3639
https://github.com/w3c/remote-playback/issues/137
@@ -41,6 +44,7 @@ spec:webidl; type:dfn; text:namespace
4144
<pre class="anchors">
4245
urlPrefix: https://dom.spec.whatwg.org/; spec: DOM
4346
url: #dom-document-getelementsbytagname; type: interface; for: Document; text: getElementsByTagName
47+
url: #dom-document-createelement; type: interface; for: Document; text: createElement
4448
urlPrefix: https://w3c.github.io/DOM-Parsing/; spec: DOM-Parsing
4549
url: #dom-innerhtml-innerhtml; type: attribute; for: Element; text: innerHTML
4650
urlPrefix: https://tc39.github.io/ecma262/; spec: ECMA262
@@ -59,6 +63,8 @@ urlPrefix: https://url.spec.whatwg.org/; spec: URL
5963
url: #percent-encode; type: abstract-op; text: percent-encode
6064
urlPrefix: https://www.w3.org/TR/payment-request/; spec: payment-request
6165
url: #dfn-state; type: dfn; text: [[state]];
66+
urlPrefix: https://www.w3.org/TR/uievents/ spec: uievents
67+
url: #idl-interface-MouseEvent-initializers; type: dfn; text: initMouseEvent
6268
</pre>
6369

6470
<style>
@@ -995,7 +1001,7 @@ converting {{undefined}} to the type of the argument.
9951001
For example, if a boolean argument isn't set,
9961002
it must default to false.
9971003

998-
When deciding between different list data types for your API,
1004+
When deciding between different list data types for your API,
9991005
unless otherwise required, use the following list types:
10001006

10011007
* Method list arguments should be of type [=sequence types|sequence&ltT&gt=]
@@ -1112,6 +1118,32 @@ and <a href="https://github.com/heycam/webidl/issues/new">File an issue on Web I
11121118
if there is some problem with using them.
11131119
</div>
11141120

1121+
Factory methods can complement constructors, but generally should not be used *instead* of them.
1122+
It may still be valuable to include factory methods
1123+
in addition to constructors, when they provide additional benefits.
1124+
A common such case is when an API includes base classes
1125+
and multiple specialized subclasses,
1126+
with a factory method
1127+
for creating the appropriate subclass based on the parameters passed.
1128+
Often the factory method is a static method on the
1129+
closest common base subclass of the returned result.
1130+
1131+
<div class="example">
1132+
The {{Document/createElement}} method is an example of a
1133+
factory method that could not have been implemented as a constructor,
1134+
as its result can be any of a number of subclasses of {{Element}}.
1135+
If it were designed today, it may have been better to
1136+
use a static method on {{Element}} (e.g. `Element.create()`)
1137+
with the document the element is associated with passed in as a parameter.
1138+
</div>
1139+
1140+
<div class="example">
1141+
The {{Document/initMouseEvent}} factory method only creates {{MouseEvent}} objects,
1142+
which were originally not constructible,
1143+
even though there was no technical reason against that.
1144+
Eventually it was deprecated, and the {{MouseEvent}} object was simply made constructible.
1145+
</div>
1146+
11151147
<h3 id="synchronous">Use synchronous when appropriate</h3>
11161148

11171149
Where possible, prefer synchronous APIs when designing a new API.

0 commit comments

Comments
 (0)