Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm personally perefer to use <strong><code>onEventName</code></strong> names keeping native naming convention for DOM event handlers.</p> <p>Like <code>myElement.onclick = function() { /* ... */ }</code> for <code>click</code> event.</p> <p>So for <strong><code>myEvent</code></strong> I'm using handler named <strong><code>onMyEvent</code></strong>.</p> <p>And if I have event <strong><code>stateChange</code></strong>, then I'll use <strong><code>onStateChange</code></strong> handler.</p> <p>But really this question is more specific for each team of developers and <strong>code-style conventions</strong> inside the team/company.</p> <p>So the main goal in such kind of questions is to keep the code style same in all parts to ensure readability.</p> <p>Therefore if you're working in a team, just keep sticky to team's code writing conventions and if you're working alone on existing code, try to keep its code-style (sure if that style is not obviously ugly).</p> <p><strong>UPDATE: Understanding.</strong></p> <p><strong>What is the event?</strong> Roughly it's an action initiated outside or inside the program, in other words something happens in system, e.g. some state changes (the state of keyboard, of mouse, of I/O devices, etc.) doesn't matter how (the user clicked on mouse or some program sent the mouse click signal to system).</p> <p>Say the browser window is subscribed to get a notifications about some events and the operating system sending them to it as soon as possible, we'll assume that at same time when something happens. So if user clicks his mouse when the browser window is active and the document has a focus, browser says to document to fire the <code>click</code> event. And here our <code>onclick</code> handler starting its invocation. In other words the system says us that now happens a <strong>change of some state</strong>. And we're handling this <strong>change</strong> and not are handling a fact saying us that state has been changed.</p> <p><strong>Let's assume that our handler named as <code>onClicked</code>.</strong> Since the handler's name saying in past tense we can get a reasonable question: "When clicked, how long ago it happened? How many times it was clicked? Hmm, maybe it's too late to handle this action (or actions?) at all...". So this name tells us that something happened sometime in past.</p> <p><strong>In contrast when our handler named as <code>onClick</code></strong> it's obviously that <code>click</code> event just fired and fired once and we was notified about it immediately. And we're going to <strong>handle</strong> the <strong>click event</strong> - the information saying us that the state of mouse changed right now (<strong>not</strong> mouse <strong>clicked</strong>, but the <strong>event of click</strong>).</p> <p>So the names in past tense are more appropriate for the cases when we need to check if some state has been changed or not. E.g. if the variable stores the <code>state = 1</code> we can call the function <strong><code>isStateChanged();</code></strong> which will compare the value in <code>state</code> variable with the real value at the current moment. And here the past tense is good choice for naming.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload