37

A glimpse into the mind of a JavaScript framework author

 2 years ago
source link: https://levelup.gitconnected.com/a-glimpse-into-the-mind-of-a-javascript-framework-author-689216a8619c?source=friends_link&%3Bsk=07e9214acd5e7703ca9c19a526a1f250&%3Bgi=a2b652ff1c66
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

EBZRFbU.jpg!web

Photo by Markus Spiske on Unsplash

Have you ever read the source code of a popular library and come across comments which explain what the code is doing? Sometimes the explanation given is straightforward but other times it leaves you scratching your head. I had one such moment recently and after much head scratching, it left me with a deeper appreciation of the effort that goes into open source tools and inspired me to write this post.

I came across the code in question whilst investigating how frontend frameworks attach event handlers to DOM elements. Over the last few months, I have been rebuilding different parts of the frontend stack as a way of improving my knowledge, and part of that has involved creating a UI framework based on the virtual DOM paradigm.

The code I was looking at was from Mithril . Internally, it registers events on DOM elements by creating an object and passing it as the second argument to the document.addEventListener method. The relevant source code is:

The third point: “ The object does not inherit from Object.prototype , to avoid any potential interference with that (e.g. setters). ” is what caught my attention. I reached out to Mithril core maintainer Isiah Meadows via the Mithril Gitter chat and his explanation is what we will go into next.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK