Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
jQuery Bad Practices
Search
Filipe Costa
November 09, 2013
Programming
2
240
jQuery Bad Practices
Let's learn some bad jQuery practices to avoid them.
Filipe Costa
November 09, 2013
Tweet
Share
More Decks by Filipe Costa
See All by Filipe Costa
Lidando com Efeitos Colaterais com Redux Saga
filipebarcos
0
200
Pitch - Lidando com Efeitos Colaterais com Redux Saga
filipebarcos
0
360
Rust for Rubysts
filipebarcos
2
240
Limpando Seu Código JS Com O Padrão Pub/Sub
filipebarcos
0
180
Tu trabalha em casa?? Que moleza hein!
filipebarcos
0
120
Rediscovering OOP in Rails World
filipebarcos
0
89
Intro to Ruby
filipebarcos
1
120
Other Decks in Programming
See All in Programming
GraphRAGの仕組みまるわかり
tosuri13
7
480
型付きアクターモデルがもたらす分散シミュレーションの未来
piyo7
0
810
ドメインモデリングにおける抽象の役割、tagless-finalによるDSL構築、そして型安全な最適化
knih
11
2k
関数型まつりレポート for JuliaTokai #22
antimon2
0
150
ruby.wasmで多人数リアルタイム通信ゲームを作ろう
lnit
2
260
PHPでWebSocketサーバーを実装しよう2025
kubotak
0
150
PHP 8.4の新機能「プロパティフック」から学ぶオブジェクト指向設計とリスコフの置換原則
kentaroutakeda
2
520
Gleamという選択肢
comamoca
6
760
Deep Dive into ~/.claude/projects
hiragram
8
1.5k
AIコーディング道場勉強会#2 君(エンジニア)たちはどう生きるか
misakiotb
1
250
ReadMoreTextView
fornewid
1
480
iOSアプリ開発で 関数型プログラミングを実現する The Composable Architectureの紹介
yimajo
2
210
Featured
See All Featured
Speed Design
sergeychernyshev
32
1k
Optimising Largest Contentful Paint
csswizardry
37
3.3k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
Become a Pro
speakerdeck
PRO
28
5.4k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.8k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
930
Mobile First: as difficult as doing things right
swwweet
223
9.7k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
181
53k
Agile that works and the tools we love
rasmusluckow
329
21k
Testing 201, or: Great Expectations
jmmastey
42
7.5k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Transcript
jQuery Bad Practices Filipe Costa
Filipe Costa 4UBDL#VJMEFST @filipebarcos BCPVUNF pMJQFCBSDPT
DISCLAIMER
Really?? jQuery?!
https://github.com/search?l=Ruby&q=jquery&ref=advsearch&type=Repositories https://github.com/search?l=Python&q=jquery&ref=advsearch&type=Repositories https://github.com/search?l=JavaScript&q=jquery&ref=advsearch&type=Repositories
People say sh** about jQuery
Obtrusive Hard to follow Hard to maintain Disorganized Bloated Slow
Difficult to talk about
;(function(window, document, $) {! var isInputSupported = 'placeholder' in document.createElement('input');!
var isTextareaSupported = 'placeholder' in document.createElement('textarea');! var prototype = $.fn;! var valHooks = $.valHooks;! var propHooks = $.propHooks;! var hooks;! var placeholder;! ! if (isInputSupported && isTextareaSupported) {! placeholder = prototype.placeholder = function() {! return this;! };! placeholder.input = placeholder.textarea = true;! } else {! ! placeholder = prototype.placeholder = function() {! var $this = this;! $this.filter((isInputSupported ? 'textarea' : ':input') + '[placeholder]')! .not('.placeholder')! .bind({! 'focus.placeholder': clearPlaceholder,! 'blur.placeholder': setPlaceholder! }).data('placeholder-enabled', true).trigger('blur.placeholder');! return $this;! };! placeholder.input = isInputSupported;! placeholder.textarea = isTextareaSupported;! hooks = {! 'get': function(element) {! var $element = $(element);! var $passwordInput = $element.data('placeholder-password');! if ($passwordInput) {! return $passwordInput[0].value;! }! return $element.data('placeholder-enabled') && $element.hasClass('placeholder') ? '' : element.value;! },! 'set': function(element, value) {! var $element = $(element);! var $passwordInput = $element.data('placeholder-password');! if ($passwordInput) {! return $passwordInput[0].value = value;! }! if (!$element.data('placeholder-enabled')) {! return element.value = value;! }!
None
None
Caching
function whatever() {! $(".container input#elem").attr("title", $(".container input#elem").text());! $(".container input#elem").css("color", "red");!
$(".container input#elem").fadeOut();! }
This is BAD!
function whateverBetter() {! var elem = $("#elem");! elem.attr("title", elem.text());! elem.css("color",
"red");! elem.fadeOut();! }
We can do better than that
function whateverEvenBetter() {! var elem = $("#elem");! elem.attr("title", elem.text())! .css("color",
"red")! .fadeOut();! }
Querying DOM
Yo
$("div.data .user"); $(".data section.user");
Excessive Specificity
$(".data table.users td.user"); $(".data td.user");
Avoid Sizzle http://learn.jquery.com/performance/optimize-selectors/
$('#content p.title').fadeIn(); $('#content').find('p.title').fadeIn();
Avoid Universal Selectors
$(".countries :selected"); $(".countries input:selected");
Context
$('.selectbox-user').toggle(); $('.selectbox-user', '#user-table').toggle();
$(this) “jQuery way"
$('.input-field').on('focus', function()) {! console.log($(this).val());! } $('.input-field').on('focus', function()) {! console.log(this.value);! }
DRY
function whateverEvenBetter() {! var elem = $("#elem");! elem.attr("title", elem.text())! .css("color",
"red")! .fadeOut();! }
AJAX
function getCities(state) {! $.ajax({! url: '/cities',! type: 'get',! data: state,!
success: function(data) {! $('.user-form-states-select').html(data);! }! });! }
That’s just the beginning
//functions.js! function getCities(state) {! return $.ajax({! url: '/cities',! type: 'get',!
data: state! });! }! ! //user.js! var FillSelectBox = function(data) {! $('.user-form-state-select').html(data);! }! ! getCities('CE').then(FillSelectBox);
Optimisation
jQuery won’t take care of UR dumbness
var names = ["Greg", "Peter", "Kyle", "Danny", "Mark"];! $.each(names, function(index,
value) {! $("ul.people").append("<li id=" + index + ">" + value + "</li>");! });
Caching again?
var names = ["Greg", "Peter", "Kyle", "Danny", "Mark"],! list =
$("ul.people");! $.each(names, function(index, value) {! list.append("<li id=" + index + ">" + value + "</li>");! });
We can do better than that
var names = ["Greg", "Peter", "Kyle", "Danny", "Mark"],! ! list
= $("ul.people"),! ! listItems = "";! ! $.each(names, function(index, value) {! listItems += "<li id=" + index + ">" + value + "</li>";! });! ! list.append(dynamicItems);
Safety
NOPE
var j = jQuery.noConflict();
jQuery(document).ready(function($) { ! //your code here! });
But U R cool
So U ain’t use jQuery
jk
(function($) {! //your code here! })(jQuery);
</code>
I use jQuery and I’m still cool
jQuery is your FRIEND
Learn how to use it
jQuery is just a tool
USUALLY, it’s not tool's fault
Thank you! Filipe Costa @filipebarcos