Skip to content

Commit 53fe1f5

Browse files
committed
docs: further refine method name form with warnings
1 parent fa18fe9 commit 53fe1f5

1 file changed

Lines changed: 26 additions & 6 deletions

File tree

docs/_guide/anti-patterns.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,43 @@ class UserSettingsElement extends HTMLElement {
117117
When naming a method, you should avoid naming it something that already exists on the `HTMLElement` prototype; as doing so can lead to surprising behaviors. Test out the form below to see what method names are allowed or not:
118118

119119
<form>
120-
<h4>I want my method to be called...</h4>
121-
122-
<input class="js-methodname-shadow-test mb-4">
120+
<label>
121+
<h4>I want my method to be called...</h4>
122+
<input class="js-methodname-shadow-test mb-4">
123+
</label>
123124
<div hidden class="js-methodname-shadow-bad-input text-red">
124125
{{ octx }} This name would shadow <code></code>, you'll need to pick a different name
125126
</div>
127+
<div hidden class="js-methodname-shadow-warn-input text-orange-light">
128+
{{ octx }} While this name is allowed, it's not ideal because <span></span>. You should consider a different name.
129+
</div>
126130
<div hidden class="js-methodname-shadow-good-input text-green">
127131
{{ octick }} This is a good name for a method!
128132
</div>
129133
<script>
134+
const warnings = {
135+
'new': 'it has a special meaning in JS',
136+
'super': 'it has a special meaning in JS',
137+
'prototype': 'it has a special meaning in JS',
138+
'requestSubmit': 'it is a proposed new feature',
139+
}
130140
document.querySelector('.js-methodname-shadow-test').addEventListener('input', () => {
131141
const name = event.target.value
132142
const goodEl = document.querySelector('.js-methodname-shadow-good-input')
133143
const badEl = document.querySelector('.js-methodname-shadow-bad-input')
134-
goodEl.hidden = (name in HTMLElement.prototype)
135-
badEl.hidden = !(name in HTMLElement.prototype)
136-
if (name in HTMLElement.prototype) {
144+
const warnEl = document.querySelector('.js-methodname-shadow-warn-input')
145+
let warning = warnings[name]
146+
if (name !== name.toLowerCase() && name.toLowerCase() in HTMLElement.prototype) {
147+
warning = `it is too similar to \`${name.toLowerCase()}\` which already exists`
148+
} else if (name.startsWith('on') && !(name in HTMLElement.prototype)) {
149+
warning = 'starting with `on` suggests a coupling between the event and the method (see below)'
150+
}
151+
goodEl.hidden = warning || (name in HTMLElement.prototype)
152+
warnEl.hidden = !warning
153+
badEl.hidden = warning || !(name in HTMLElement.prototype)
154+
if (warning) {
155+
warnEl.querySelector('span').textContent = warning
156+
} else if (name in HTMLElement.prototype) {
137157
let proto = HTMLElement.prototype
138158
while(proto !== null) {
139159
if (proto.hasOwnProperty(name)) break

0 commit comments

Comments
 (0)