48
leave your ‘$’ behind / Fabien Doiron @fabien_doiron

Leave your jQuery behind

Embed Size (px)

DESCRIPTION

Slides from my talk at Confoo 2014. Notes to come... In the not-so-distant past, cross browser JavaScript support was painful to deal with. This lead to the rise in popularity of libraries and a lot of developers have since never looked back. This talk will explain the importance of learning native JavaScript and how libraries may not be the answer in the near future. ## Resources * focusout: https://bugzilla.mozilla.org/show_bug.cgi?id=687787 * http://youmightnotneedjquery.com/ * https://gist.github.com/rwaldron/8720084#file-reasons-md * http://addyosmani.com/resources/essentialjsdesignpatterns/book/#facadepatternjavascript * http://caniuse.com/ ## Links * http://fabien-d.github.io/ * http://twitter.com/fabien_doiron * http://canvaspop.com * http://dna11.com * http://crated.com * http://developers.canvaspop.com * http://remade.canvaspop.com/

Citation preview

Page 1: Leave your jQuery behind

leave your ‘$’ behind

/ Fabien Doiron @fabien_doiron

Page 2: Leave your jQuery behind

I am not here to tell you to

stop using jQuery

Page 3: Leave your jQuery behind

I work on Cratedsoftware developer, front-end

@fabien_doiron

Page 5: Leave your jQuery behind

compatibility &unexpected results

Page 6: Leave your jQuery behind

backwards compatibility

Object.create

Chrome Firefox Internet Explorer Opera Safari

Yes 4+ 9+ 12+ 5+

Page 7: Leave your jQuery behind

include a polyfill, don’t use the feature orignore the unsupported browsers

Page 8: Leave your jQuery behind

cross browser compatibility

focusout

Chrome Firefox Internet Explorer Opera Safari

Yes No Yes Yes Yes

Page 9: Leave your jQuery behind

bug has been opened for over

2.5 years

Page 10: Leave your jQuery behind

unexpected results

var input = document.createElement( 'input' );

input.value = 'confoo2014';

input.setAttribute( 'type', 'radio' );

console.log( input.value === 'confoo2014' );

Chrome Firefox Internet Explorer Opera Safari

true true false IE10-

true? IE11

true true

Page 11: Leave your jQuery behind

“value” attribute must be set

after “type” attribute

Page 12: Leave your jQuery behind

server-side vs client-side

Page 13: Leave your jQuery behind

server-side code

limited by what the server supports

works in 1 spot, works everywhere

client-side code

limited by what the client (e.g. browser) supports

works in 1 spot, hope it works everywhere

Page 14: Leave your jQuery behind

“One cries because one is sad.

For example, I cry because browsers are

inconsistent and that makes me sad.”

Page 15: Leave your jQuery behind

rise of libraries

Page 16: Leave your jQuery behind

“It makes things like HTML document traversal and

manipulation, event handling, animation, and Ajax much

simpler with an easy-to-use API that works across a multitude

of browsers” - jQuery

Page 17: Leave your jQuery behind

“It makes things like HTML document traversal and

manipulation, event handling, animation, and Ajax much

simpler with an easy-to-use API that works across a multitude

of browsers” - jQuery

Page 18: Leave your jQuery behind

back to the start

Page 19: Leave your jQuery behind

2006jQuery launched

Page 20: Leave your jQuery behind

20148 years later

Page 21: Leave your jQuery behind

0%of browsers in 2006 supported

querySelector()

Page 22: Leave your jQuery behind

100%*of browsers in 2014 support

querySelector()

*current version and up to 3 versions back

Page 23: Leave your jQuery behind
Page 24: Leave your jQuery behind

jQuery: pros

tested and proven

large community

easy to use API

tons of browser specific edge cases

filesize is reasonable

Page 25: Leave your jQuery behind

jQuery: cons

bloat, can be more than you need

native support is on the rise

can lead to a hard dependency

can lead to poor engineering decisions

Page 26: Leave your jQuery behind

abstractionfacade pattern

Page 27: Leave your jQuery behind

“hide all the unwanted information and

provide only what is needed”

Page 28: Leave your jQuery behind

in other words

write your own API

Page 29: Leave your jQuery behind

before we continue

I am not saying you have to write a new library

Page 30: Leave your jQuery behind

write code that is not tied to any library

Page 31: Leave your jQuery behind

basic example

$( '.conference' ).addClass( 'confoo2014' );

// <div class="conference confoo2014">…</div>

Page 32: Leave your jQuery behind

custom API w/ jQuery var myui = {};

/** * Return first matching element * @requires jQuery … */ myui.qs = function querySelector ( selector ) { return $( selector )[ 0 ]; };

/** * Add class to passed element * @requires jQuery … */ myui.addClass = function addClass ( el, klass ) { $( el ).addClass( klass ); };

*basic example to show the concept

Page 33: Leave your jQuery behind

using jQuery through API

var el = myui.qs( '.conference' );

myui.addClass( el, 'confoo2014' );

// <div class="conference confoo2014">…</div>

Page 34: Leave your jQuery behind

custom API w/ native JavaScript var myui = {};

/** * Return first matching element * @requires N/A … */ myui.qs = function querySelector ( selector ) { return document.querySelector( selector ); };

/** * Add class to passed element * @requires N/A … */ myui.addClass = function addClass ( el, klass ) { el.classList.add( klass ); };

*basic example to show the concept

Page 35: Leave your jQuery behind

using native JavaScript through API

var el = myui.qs( '.conference' );

myui.addClass( el, 'confoo2014' );

// <div class="conference confoo2014">…</div>

Page 36: Leave your jQuery behind

source code does not have to change

jQuery JavaScript

var el = myui.qs( '.conference' );

myui.addClass( el, 'confoo2014' );

// <div class="conference confoo2014">…</div>

myui.qs

return $( selector )[ 0 ];

myui.addClass

$( el ).addClass( klass );

myui.qs

return document.querySelector( selector );

myui.addClass

el.classList.add( klass );

Page 37: Leave your jQuery behind

browser consistency is on the rise

prepare yourself for it

Page 38: Leave your jQuery behind

drawbacks

Page 39: Leave your jQuery behind

heavy upfront lift

Page 40: Leave your jQuery behind

abstracting the abstraction

Page 41: Leave your jQuery behind

potential performance issue

Page 42: Leave your jQuery behind

dealing with edge cases

Page 43: Leave your jQuery behind

recap

Page 44: Leave your jQuery behind

don’t let anyone tell you to stop using jQuery

JavaScript development has compatibility issues

JavaScript development has unexpected results

libraries deal with these problems

lots of pros for using jQuery

lots of cons for using jQuery

consider abstraction

hide the fact that you use a library

there are some drawbacks

Page 45: Leave your jQuery behind

only you can decide if

jQuery is the right option

Page 47: Leave your jQuery behind

CONFOO2014

40% offcoupon code because you were awesome

*valid until March 30, 2014

Page 48: Leave your jQuery behind

thank you

questions

/ Fabien Doiron @fabien_doiron