Removing jQuery from frontend

编程之家收集整理的这篇文章主要介绍了Removing jQuery from frontend编程之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Removing jQuery from frontend 

Web standards in the later years

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:

  • The $(selector) pattern can easily be replaced with querySelectorAll();
  • CSS classname switching can now be achieved using Element.classList;
  • CSS now supports defining visual animations in stylesheets rather than in JavaScript;
  • $.ajax requests can be performed using the Fetch Standard;
  • The addEventListener() interface is stable enough for cross-platform use;
  • We could easily encapsulate the event delegation pattern with a lightweight library;
  • Some syntactic sugar that jQuery provides has become reduntant with the evolution of JavaScript language.

Furthermore, the chaining Syntax didn’t satisfy how we wanted to write code going forward. For example:


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.


Incremental decoupling

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:

  • Set up metrics that tracked ratio of jQuery calls used per overall line of code and monitored that graph over time to make sure that it’s either staying constant or going down, not up.

    Graph of jQuery usage going down over time.

  • We discouraged importing jQuery in any new code. To facilitate that using automation, we created eslint-plugin-jquery which would fail CI checks if anyone tried to use jQuery features, for example $.ajax.
  • There were now plenty of violations of eslint rules in old code, all of which we’ve annotated with specific 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.
  • We created a pull request bot that would leave a review comment on a pull request pinging our team whenever somebody tried to add a new eslint-disable rule. This way we would get involved in code review early and suggest alternatives.
  • A lot of old code had explicit coupling to external interfaces of pjax and faceBox jQuery plugins, so we’ve kept their interfaces relatively the same while we’ve internally replaced their implementation with vanilla JS. Having static type checking helped us have greater confidence around those refactorings.
  • Plenty of old code interfaced with rails-behaviors, our adapter for the Ruby on Rails approach to “unobtrusive” JS, in a way that they would attach an AJAX lifecycle handler to certain forms: 



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 frontend全部内容,希望文章能够帮你解决Removing jQuery from frontend所遇到的程序开发问题。





目录验证码实现效果如下:生成验证码的方法:生成验证码随机背景颜色在html中引用完整源码下载地址:验证码实现效果如下:生成验证码的方法:function code_draw() { var canvas_width = $('#canvas').width(); var canvas_height = $('#canvas').height(); var canvas = document.getElementById("canvas"); //获取到canvas
比如info是一个字符串变量,如果需要去该变量的值,需要使用下面的语句:语法:alert($("#"+id).val());例如:使用id设置隐藏控件 $(".img").mouseover(function(event){ var; //alert(html); var aid=html.match(/<a id="(...
使用js去除字符串内所带有空格,有以下三种方法:( 1 ) replace正则匹配方法去除字符串内所有的空格:str = str.replace(/s*/g,"");去除字符串内两头的空格:str = str.replace(/^s*|s*$/g,"");去除字符串内左侧的空格:str = str.replace(/^s*/,"");去除字符串内右侧的...
1.开启disabled,是input不可以编辑$("#input_id").attr("disabled","disabled");2.关闭disabled$("#input_id").removeAttr("disabled");普通js中是这样写的,document.getElementById("input_id").disabled = false;...
最近自学了一段时间的HTML、CSS、JavaScript、jQuery。通过自己这段时间学到的一点小知识,自己制作了一个小app------简易备忘录在此简单记录一下,同时也希望有更好创意的朋友提出改进意见或者指++++++++++++++++++++++++++++++++++++++++++++++++++++++++++开发工具:HBui 链(Chaining)通过jQuery,可以把动作/方法链接在一起。Chaining允许我们在一条语句中运行多个jQuery方法(在相同的元素上)。jQuery方法链接直到现在,我们都是一次写一条jQuery语句(一条接着另一条)。不过,有一
一、宽度和高度获取宽度.width()描述:为匹配的元素集合中获取第一个元素的当前计算宽度值。这个方法不接受任何参数。.css(width) 和 .width()之间的区别是后者返回一个没有单位的数值(例如,400),前者是返回带有完整单位的字符串(例如,400px)。当一个元素的宽度需要数学计算的时候推