Implementing classes in Javascript I'm building a simple chatApp. The UI has just 3 interaction areas: input, submit and previous messages button.
But I'm not interested in reviewing the app architecture as much as the javascript ideas and general understanding/errors.
I've commented the code, and would like to see if you can point out any bad practice. I'm sure most of the code is bad, but still, the most important errors.
I've also included the UI as a runnable snippet.
Any edits to make it better for reviewers will be done.


&lt;!doctype html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Socket.IO chat&lt;/title&gt;
    &lt;style&gt;
    * { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: 0.5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }

#wantMore{
  position:absolute;
  top:0;
  right:0;
}
&lt;/style&gt;
    &lt;/head&gt; 
  &lt;body&gt;
    &lt;button id="wantMore"&gt;More&lt;/button&gt;
    &lt;ul id="messages"&gt;&lt;/ul&gt;
    &lt;form action=""&gt;
      &lt;input id="m" autocomplete="off" /&gt;&lt;button&gt;Send&lt;/button&gt;
    &lt;/form&gt;
    &lt;script src="mySnippet"&gt;&lt;/script&gt;
    &lt;script src="socket-io.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;



const prevMsgs = document.querySelector(&quot;button#wantMore&quot;)
const uri = &quot;http://localhost:3000&quot;

class Msg {
  constructor (msg, createdAt){
    this.msg = msg
    this.createdAt = createdAt
  }
static toHTML ({msg, createdAt}) {
// pass object, builds list item.
  try{
  //in case the date conversion fails.
    createdAt = (new Date(createdAt))
      .toTimeString()
      .substr(0,5)
  } catch(e){ console.log(e) } 
  finally{ createdAt || &quot;&quot; }
return `&lt;li&gt;&lt;span&gt;${msg}&lt;/span&gt; &amp;nbsp; &lt;span&gt;${createdAt}&lt;/span&gt;&lt;/li&gt;`
}

static put (data) {
  $(&quot;#messages&quot;).prepend(this.toHTML(data))
}

static getMsgs (uri, skip, limit) {
  //returns a promise, errors are catch
  return fetch(`${uri}/messages/skip/${skip}/limit/${limit}`)
    .then(data =&gt; data.json())
    .then(items =&gt; items.forEach(item =&gt; this.put(item)))
}

static getLastMsgs (uri) { return this.getMsgs(uri, 0,10) }

}


$(function () { // $(document).ready(
  
  Msg.getLastMsgs(uri).catch(e =&gt; console.log(e))

  prevMsgs.addEventListener(&quot;click&quot;, () =&gt; {
      const msgLen = parseInt($(&quot;#messages&quot;).children().length)
      Msg.getMsgs(uri, msgLen, msgLen+10)
    })

  const socket = io(); //connects to / origin

  $('form').submit(function(e) {
    e.preventDefault(); // prevents page reloading

    const msg = new Msg($('#m').val(), new Date())

    //for the sender, append right away.
    $(&quot;#messages&quot;).append(Msg.toHTML(msg))
    // and emit, and broadcast
    socket.emit('chat message', msg);
    $('#m').val('');
    return false;
  });
  
  socket.on('chat message', data =&gt; {
    $(&quot;#messages&quot;).append(Msg.toHTML(data)) 
  })
})

```
Be careful of IDs, especially those with common names, since they (unfortunately) automatically create properties on the global object when the ID is a valid identifier, which is not an uncommon source of bugs 2. Here, you have #messages, which is probably a pretty common word related to your script. It's not likely to be a problem in most cases, but I'd feel more comfortable using a class instead.
CSS tweak Due to positioning, the send button appears to have an unusually thick border on the right. Consider using width: 9.5%; instead of width: 9%;. You could also consider using flex for the &lt;form&gt; instead, and only set an explicit width of a few % for the button, letting the &lt;input&gt; horizontally expand to take up the rest of the space automatically, without hard-coding.
jQuery? You have a few uses of $ that look like uses of jQuery, but it doesn't appear to exist as a &lt;script&gt; in the HTML. Make sure to include it. If jQuery is included via a build process like Webpack (which is a perfectly reasonable approach) but simply not listed in the code here, consider also including socket.io the same way, for consistency and to reduce the number of requests the client has to make before the page becomes operational.
If you are indeed intending to use jQuery, then for stylistic consistency, consider using it for DOM selection and event listeners everywhere, rather than sometimes using jQuery and sometimes using querySelector and addEventListener.
Indentation Consistent indentation improves readability, when one can understand the { } blocks at a glance. Lots of IDEs have an auto-formatting feature, consider taking advantage of it. Eg
  }
static toHTML ({msg, createdAt}) {
// pass object, builds list item.
  try{

should be
  }
  static toHTML ({msg, createdAt}) {
    // pass object, builds list item.
    try {

Creation of invalid dates won't throw - the try/catch won't do anything. Worst case, createdAt will end up containing the string 'Inval' (from Invalid Date). The finally has an unused expression too - did you mean to assign to createdAt maybe? To check if the date is invalid, you could either check if the time string is Invalid Date first, or see if getTime returns NaN.
Msg class That whole class seems odd to me, since it never uses its instance properties and only has static methods. The fact that the structure is a class isn't providing any organizational benefit for the code. Consider either:

Use at least some prototype methods for the class instead, when the method uses data that should exist on an instance - so, toHTML and put. (put isn't a very informative name, maybe rename to insertIntoDOM or renderMessage)
Remove the class entirely and use a plain array of objects and plain functions instead

Be careful when concatenating HTML with user input, because it can result in arbitrary code execution. With this:
`&lt;li&gt;&lt;span&gt;${msg}&lt;/span&gt;...

If msg contains unsafe code (either posted by another user, or if this user has been tricked into entering unsafe code), the current user could have all available sensitive data sent to a malicious third party.
To fix it, create the &lt;span&gt; without any content at first, then assign to its text content afterwards, maybe something like:
// createdAt looks like it should always be trustworthy, so interpolation is OK
const $li = $(`&lt;li&gt;&lt;span&gt;&lt;/span&gt; &amp;nbsp; &lt;span&gt;${createdAt}&lt;/span&gt;&lt;/li&gt;`);
$li.find('span').text(msg);
return $li;

Catch errors If the fetch fails, it will not be caught if initiated from a click. Add a .catch onto Msg.getMsgs(uri, msgLen, msgLen+10). In both this new .catch and the existing .catch on the getLastMsgs call, when there's an error, it'd be user-friendly to display the fact that there was an error to the user, perhaps in a popup, rather than just logging. After all, while developers look at the console to debug, normal users don't.