标签:library this represent def es6 asc sed style The
Over the years, GitHub grew into a company with hundreds of engineers and a dedicated team gradually formed to take responsibility for the size and quality of JavaScript code that we serve to web browsers. One of the things that we’re constantly on the lookout for is technical debt, and sometimes technical debt grows around dependenices that once provided value, but whose value dropped over time.
When it came to jQuery, we compared it against the rapid evolution of supported web standard in modern browsers and realized:
$(selector)
pattern can easily be replaced with querySelectorAll()
;$.ajax
requests can be performed using the Fetch Standard;addEventListener()
interface is stable enough for cross-platform use;Furthermore, the chaining syntax didn’t satisfy how we wanted to write code going forward. For example:
$(‘.js-widget‘)
.addClass(‘is-loading‘)
.show()
This syntax is simple to write, but to our standards, doesn’t communicate intent really well. Did the author expect one or more js-widget
elements on this page? Also, if we update our page markup and accidentally leave out the js-widget
classname, will an exception in the browser inform us that something went wrong? By default, jQuery silently skips the whole expresion when nothing matched the initial selector; but to us, such behavior was a bug rather than a feature.
Finally, we wanted to start annotating types with Flow to perform static type checking at build time, and we concluded推断 that the chaining syntax doesn’t lend itself well to static analysis, since almost every result of a jQuery method call is of the same type. We chose Flow over alternatives because, at the time, features such as @flow weak
mode allowed us to progressively and efficiently start applying types to a codebase which was largely untyped.
All in all, decoupling from jQuery would mean that we could rely on web standards more, have MDN web docs be de-facto实际上的 default documentation for our frontend developers, maintain more resilient code in the future, and eventually drop a 30 kB dependency from our packaged bundles, speeding up page load times and JavaScript execution times.
Even with an end goal in sight, we knew that it wouldn’t be feasible to just allocate all resources we had to rewriting everything from jQuery to vanilla JS. If anything, such a rushed endeavor would likely lead to many regressions in site functionality that we would later have to weed out. Instead, we:
$.ajax
.eslint-disable
rules in code comments. To the reader of that code, those comments would serve as a clear signal that this code doesn’t represent our current coding practices.eslint-disable
rule. This way we would get involved in code review early and suggest alternatives.
These are the polyfills that helped us transition to using standard browser features. We try to serve most of these polyfills only when absolutely necessary, i.e. to outdated browsers as part of a separate “compatibility” JavaScript bundle.
Removing jQuery from GitHub.com frontend
标签:library this represent def es6 asc sed style The
原文地址:https://www.cnblogs.com/chucklu/p/11162014.html