今更な記事ですがご容赦ください。
フロントエンジニアさんにとってよくお目にかかれるjquery。サーバーへかける負担もあってかほとんどの方がCDNでscriptに記述しています。
手軽にajaxを使ってデータを出し入れできるので、新しいライブラリが出てくる中でもまだまだ第一線で活躍しています。個人的な考え
になりますが、ライブラリをいちいち参照しているなら自前でライブラリを持っているfetchと交代したほうがいいのかなと思い、試しに
fetchやaxiosの三つ巴で速度を軽く計測していたら三つともそこまで変わらなかったです。意外・・・。
むしろレイアウトができるまでならfetchの方が遅かったくらいなのでしばらく参りましたしか言えませんでした。という訳でフロント開
発時にはまだまだお世話になることになりそうです。
コードのリファクタリングも進んできたのでそろそろjqueryのバージョンを現行バージョンに合わせようかと思いjquery3とjquery2の違い
を色々ググってみました。すると気になるトピックがありました。どうやら、jquery3に移行した場合は今まで使っていた
success/error/cpmpleteが使えなくなり、代替えとしてdone/fail/alwayまたはpromiseを含んだthen/catchメソッドを使わないといけなくなるみたいです。
</pre> <strong>perhaps deprecated</strong> success: function(xml, textStatus, xhr) { }, error: function(xhr, textStatus, error) { } <pre>
</pre> <strong>from now on</strong> <pre><span class="p">.</span><span class="nx">done</span><span class="p">((</span><span class="nx">data</span><span class="p">,</span> <span class="nx">textStatus</span><span class="p">,</span> <span class="nx">jqXHR</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="p">})</span> <span class="p">.</span><span class="nx">fail</span><span class="p">((</span><span class="nx">jqXHR</span><span class="p">,</span> <span class="nx">textStatus</span><span class="p">,</span> <span class="nx">errorThrown</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="p">})</span> <span class="p">.</span><span class="nx">always</span><span class="p">(()</span> <span class="o">=></span> <span class="p">{</span> <span class="p">});</span></pre> <pre>
linkBreaking change: Special-case Deferred methods removed from jQuery.ajax
The jqXHR
object returned from jQuery.ajax()
is a jQuery Deferred
and has historically had three extra methods with names matching the arguments object of success
, error
, and complete
. This often confused people who did not realize that the returned object should be treated like a Deferred
. As of jQuery 3.0 these methods have been removed. As replacements, use the Deferred
standard methods of done
, fail
, and always
, or use the new then
and catch
methods for Promises/A+ compliance.
Note that this does not have any impact at all on the ajax callbacks of the same name passed through the options
object, which continue to exist and are not deprecated. This only affects the jqXHR methods.
success/errorももう5年前くらいの書式なのでそろそろみなさんこの書き方にしましょうねという無言の圧力に感じますがどうなんでしょ
うか。こっちの書き方のほうがシンプルで視覚的に分かりやすいので廃止になってほしくないのですが・・・。
これだけ推したにも関わらず速度やサーバーにかける負荷がそこまで大きくないなら現行のバージョン2でもいいのではと思ってしまいました。
いずれにしろ「ajax」をこれからも使おうとする場合は細かいアップデートに順応していく必要があると感じました。