Download - Vanilla JS*

Transcript
Page 1: Vanilla JS*

Vanilla JS*

Théodore 'nod_' BiadalaJS Maintainer for Drupal core

DrupalCon, Amsterdam 2014

Page 2: Vanilla JS*

Vanilla JS*

Context

Page 3: Vanilla JS*

D8 Vanilla JS

active-link.jsannounce.js

drupal.js

Page 4: Vanilla JS*

jQueryAddicted to sugar

Page 5: Vanilla JS*

Duty to the web

Browsers improved

Use it or lose it

Help make the web better

Page 6: Vanilla JS*

Drawing the lineDrawing the line

Browser support

Code cleaninessTeam knowledge

Performance–Maintainability–Time

Page 7: Vanilla JS*

D8 Polyfills

classList

matchMedia

pictureFill

Page 8: Vanilla JS*

*Falling short

Event handling still sucks

AJAX handling too

Page 9: Vanilla JS*

Micro librariesMicro libraries

Page 10: Vanilla JS*

D8 micro libraries

domready

Page 11: Vanilla JS*

Micro libraries

One featureA little bit of sugar

Smallest size possible

Page 12: Vanilla JS*

Micro libraries

Events — bean, microevent, zoe… Ajax — Reqwest, http.js, ajax…

Many more at microjs.com

Page 13: Vanilla JS*

Problems

DuplicationBugs are spread outInconsistent quality

Page 14: Vanilla JS*

Hybrid approach

Page 15: Vanilla JS*

Size of micro libraries with quality of jQuery

Page 16: Vanilla JS*

Hybrid approach

core

Event

Sizzle

AJAX

Page 17: Vanilla JS*

Custom Builds !(kb, minified + gziped)

All — 29.1Sizzle — 8.6 Core — 2.2 Event — 6.9 Ajax — 7.0

Manipulation — 6.8 Traversing — 4.1

Page 18: Vanilla JS*

Custom Builds !(kb, minified + gziped)

All — 29.1Sizzle — 8.6 Core — 2.2 Event — 6.9 Ajax — 7.0

Manipulation — 6.8 Traversing — 4.1

Page 19: Vanilla JS*

Custom Builds !(kb, minified + gziped)

All — 29.1Sizzle — 8.6 Core — 2.2 Event — 6.9 Ajax — 7.0

Event+Ajax — 10.4 Manipulation — 6.8

Traversing — 4.1

Page 20: Vanilla JS*

Static builds

Page 21: Vanilla JS*

“franken-jQuery”#1541860-62

Page 22: Vanilla JS*

Vanilla JS*

Examples

Page 23: Vanilla JS*

ES5kangax.github.io/compat-table/es5/

Page 24: Vanilla JS*

Array

[1, 2, 3, 4].forEach(doSomething);[1, 2, 3, 4].map(doSomething);[1, 2, 3, 4].filter(doSomething);

Page 25: Vanilla JS*

DOM + Array

var lis = document.querySelectorAll('li') ;Array.prototype.forEach.call(lis, doSomething) ;

Page 26: Vanilla JS*

Object

Object.keys({ index1 : 1, index2 : 2}) .forEach(doSomething) ;

Page 27: Vanilla JS*

Function

doSomething.bind(thisObject) ;

Page 28: Vanilla JS*

querySelector(All)

document.querySelectorAll('.active') ;document.querySelector('#edit-username') ;

Page 29: Vanilla JS*

classList

div.classList.remove('active');div.classList.add('active');

div.classList.toggle('element-hidden');

div.classList.contains('active');

Page 30: Vanilla JS*

DOM DOM DOM DOM

p1.insertAdjacentHTML(position, html) ;

<!-- beforebegin --><p><!-- afterbegin -->Foo<!-- beforeend --></p><!-- afterend -->

Page 31: Vanilla JS*
Page 32: Vanilla JS*

Mobile-first is Vanilla-first

Help make « Franken-jQuery »

Page 33: Vanilla JS*

Discussion

Got code ?Where do you draw the line ?

What can Drupal do to help you ?

Page 34: Vanilla JS*

Thanks !

Théodore BIADALA

@nod_

[email protected]


Top Related