JQuery is awesome, but with all major browsers following the ES and HTML5 specs, the library has become more of a convenience than a necessity for browser compatibility. While the library is useful and ubiquitous, it does distract us from learning the language that it's built on. This talk will outline functionalities that pure javascript provides, and also provide steps we can take to begin writing vanilla javascript applications and start appreciating the power and uniqueness of javascript.
2. Ditching JQuery - Hao Luo - php[tek] 2015
Intro
2
4 years as a full-time web developer
3. Ditching JQuery - Hao Luo - php[tek] 2015
I ❤ JQuery
•One codebase, all the browser!
•Introduced me to JavaScript
•“It just works”
3
4. Ditching JQuery - Hao Luo - php[tek] 2015
Goal of This Talk
•Not to convince anyone
•Important to understand the basics
•Lessen the hurdle to start using pure JavaScript
4
6. Ditching JQuery - Hao Luo - php[tek] 2015
Our Late Stay Requirements
Admin can…
•See a list of applications with some information
•Can decide to approve or deny an application
•Can delete an application
•Can add a random application
6
8. Ditching JQuery - Hao Luo - php[tek] 2015
Late Stay Workflow
8
HTTP GET latestayapp.com/purejs
HTML (empty ul#applications container)
AJAX GET latestayapp.com/applications
JSON (parses then inserts in #applications container)
(admin clicks on the approve button)
AJAX PUT latestayapp.com/applications/20/approve
JSON (update HTML)
AJAX
DOM
Events
9. Ditching JQuery - Hao Luo - php[tek] 2015
DOM
Querying, Traversal, and Manipulation
9
10. Ditching JQuery - Hao Luo - php[tek] 2015
Some DOM Operations
10
JQuery Vanilla JS
$(‘#application-‐20’); document.querySelector(‘#application-‐20’);
$el.attr(‘data-‐id’,
‘20’);
$el.attr(‘data-‐id’);
el.setAttribute(‘data-‐id’,
‘20’);
el.getAttribute(‘data-‐id’);
yes yes yes 9+
yes yes yes yes
yes yes yes 10+$el.addClass(‘approved’);
$el.removeClass(‘approved’);
$el.toggleClass(‘approved’);
el.classList.add(‘approved’);
el.classList.remove(‘approved’);
el.classList.toggle(‘approved’);
$el.data(‘id’,
‘20’);
var
id
=
$el.data(‘id’);
el.dataset.id
=
‘20’;
var
id
=
el.dataset.id; yes yes yes 11+
$button.closest(‘.application’); button.closest(‘.application’); 41 35 no no
https://dom.spec.whatwg.org/#dom-element-closestselectors
https://github.com/eligrey/classList.js/
11. Ditching JQuery - Hao Luo - php[tek] 2015
Polyfills
11
A polyfill is a piece of code that provides the technology that the developer expects the
browser to provide natively. Flattening the API landscape if you will. - Remy Sharp
var
ELEMENT
=
Element.prototype;
ELEMENT.matches
=
ELEMENT.matches
||
ELEMENT.msMatchesSelector
||
ELEMENT.mozMatchesSelector
||
ELEMENT.webkitMatchesSelector;
ELEMENT.closest
=
ELEMENT.closest
||
function
(selector)
{
var
node
=
this;
while
(node)
{
if
(node.matches(selector))
{
break;
}
node
=
node.parentElement;
}
return
node;
};
41 35 no no
yes yes yes 9+
<li
class="application"
id=“#application-‐20">
…
<div
class="action-‐bar">
<div
class=“action">
…
<button
class="delete">
</div>
</div>
</li>
deleteButton.closest('.application');
HTML
Javascript
Javascript
15. Ditching JQuery - Hao Luo - php[tek] 2015
Delegated Events
15
li.application
li.application
li.application
div#application-container
ul#applications
li.application
li.application ☺☹ ✖li.application ☺☹ ✖
JQuery
$(‘ul.applications’).on(‘click’,
‘.deleteBtn’,
deleteApplication);
Vanilla JS No
Standard
for
Event
Delegation
:(✖
✖
✖
✖
EventListener #1
is the ‘click’ target element the ‘.delete’ button?
if so, run deleteApplication
is the ‘click’ target element the ‘.approve’ button?
if so, run approveApplication
https://github.com/ftlabs/ftdomdelegate
27. Ditching JQuery - Hao Luo - php[tek] 201527
Non-Technical Reasons
• a lot of magic is confusing sometimes
• Understanding the basics makes you a better
developer
$('div');
//creates
a
jquery
instance
with
the
selection
inside
$('<div>');
//creates
a
jquery
instance
with
a
new
element
not
in
document
$($aJQueryInstance);
//makes
a
clone
of
$aJQueryInstance
$(function()
{})
//registers
function
to
run
after
DOM
is
loaded
$({foo:
'bar'});
//???
creates
a
jquery
set
that
has
a
subset
of
methods
???
var
$appsEl1
=
$('#applications');
var
$appsEl2
=
$('#applications');
$appsEl1
===
$appsEl2;
//false
var
appsEl1
=
document.querySelector('#applications');
var
appsEl2
=
document.querySelector('#applications');
appsEl1
===
appsEl2;
//true
28. Ditching JQuery - Hao Luo - php[tek] 2015
Some Takeaways
•Use Packages and Polyfills vs monolithic libraries &
frameworks
•Out of the JQuery Plugin Ecosystem and into NPM or
Bower
•Browser and Server
28
29. Ditching JQuery - Hao Luo - php[tek] 2015
When to use JQuery
•When you have to support IE8
•When you don’t have CORS
•Abstractions sometimes are good!
29
uses requestAnimationFrames
for
its
animationsuses setInterval
for
its
animations
30. Ditching JQuery - Hao Luo - php[tek] 2015
Resources for DOM references
http://blog.garstasio.com/you-dont-need-jquery/
http://youmightnotneedjquery.com/
http://html5please.com/
30