Building Frontends Thierry Sans Recipes to become a good front-end - - PowerPoint PPT Presentation

building frontends
SMART_READER_LITE
LIVE PREVIEW

Building Frontends Thierry Sans Recipes to become a good front-end - - PowerPoint PPT Presentation

Building Frontends Thierry Sans Recipes to become a good front-end developer Load Javascript code efficiently Ensure the DOM is loaded with window.onload Write good Javascript code Encapsulate Javascript in closures Create


slide-1
SLIDE 1

Building Frontends

Thierry Sans

slide-2
SLIDE 2

Recipes to become a good front-end developer

  • Load Javascript code efficiently
  • Ensure the DOM is loaded with window.onload
  • Write good Javascript code
  • Encapsulate Javascript in closures
  • Create a Frontend API
slide-3
SLIDE 3

The problem with Javascript interpreters

✓ Good Javascript is interpreted by browsers in a consistent way ๏ Bad javascript code is loosely interpreted by browsers in an inconsistent way

slide-4
SLIDE 4

Solution 1: using strict mode

➡ Force the browser to validate Javascript against the standard ✓ Dynamically raises errors (or warnings) in the console when the code is not compliant with the standard "use strict"; let doSomething = function() { // this runs in strict mode }

slide-5
SLIDE 5

Solution 2 : using JSHint

➡ Analyze Javascript source code with JSHint ✓ Statically finds bugs and reports them in the terminal

$ npm install -g jshint $ jshint myScript.js

slide-6
SLIDE 6

Problem with scoping

➡ In the browser, all Javascript files share the same execution environment i.e they share the same scope ๏ variable (and function) naming conflicts ๏ strict mode applied to all

slide-7
SLIDE 7

Scoping problem with variable names

let doSomething = function() { // first declaration of doSomething } let doSomething = function() { // conflicting doSomething from file 1 }

file1.js file2.js

slide-8
SLIDE 8

Scoping problem with strict mode

"use strict"; let doSomething = function() { // strict mode applies } let doSomethingElse = function() { // strict mode applies too }

file1.js file2.js

slide-9
SLIDE 9

Solution : encapsulate Javascript in a closure

(function() { "use strict"; let private = function() { // private is not available from outside } }());

slide-10
SLIDE 10

Solution : encapsulate and export the namespace

let $ = (function() { "use strict"; let module = {}; let private = function(){ // private is not available from outside } module.public = function(){ // public is available from outside } return module; }());

slide-11
SLIDE 11

Structuring the frontend

Frontend API Controller

UI events UI update Push & Pull

Backend Web API