Top Banner
JavaScript LevelUp
23

JavaScript LevelUp by Lee Brandt

Jan 23, 2018

Download

Technology

Sigma Software
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JavaScript LevelUp by Lee Brandt

JavaScript LevelUp

Page 2: JavaScript LevelUp by Lee Brandt

Ласкаво просимо

Page 3: JavaScript LevelUp by Lee Brandt

Who is THIS freakin’ guy?

Page 4: JavaScript LevelUp by Lee Brandt

Lee Brandt

Director R&D @PaigeLabs

Programmer

Teacher

Star of Finding Bigfoot

JavaScript, All day, every day

Keeper of Keys and Grounds @Hogwarts

Page 5: JavaScript LevelUp by Lee Brandt

IDoSomethingAwesomeJS

Page 6: JavaScript LevelUp by Lee Brandt

Our Agenda

Scope and Closure

Prototypal Inheritance

Modularity

JavaScript Quirks

Tips & Tricks

Asynchronicity

Page 7: JavaScript LevelUp by Lee Brandt

Lexical Scopefunction outerFunction() {

var foo = 5;

function innerFunction(){

foo++;

var bar = 5;

}

console.log(bar);

}

Page 8: JavaScript LevelUp by Lee Brandt

Closure

var add = (function(){

var counter = 0;

return function(number){

console.log(counter += number);

};

})();

for (var i = 0; i < 10; i++) {

add(1);

}

Page 9: JavaScript LevelUp by Lee Brandt

Prototypal Delegation

Object.prototype

.toString()

.valueOf()

.hasOwnProperty()

Page 10: JavaScript LevelUp by Lee Brandt

Prototypal Delegation

var Foo = function(){

// magic

};

var a = new Foo();

var a = Object.create(Foo);

Page 11: JavaScript LevelUp by Lee Brandt

Modularity (AMD)

define(function(){

return function CallMe(){

console.log('Inside module one.');

};

});

define(['FirstModule'], function(mod1){

mod1();

});

FirstModule.js

Main.js

Page 12: JavaScript LevelUp by Lee Brandt

Modularity (CommonJS)

module.exports = function callMe(){

console.log(‘called’);

};

var thingie = require(‘./FirstModule');

thingie();

FirstModule.js

Main.js

Page 13: JavaScript LevelUp by Lee Brandt

Why So Asynchronous?

Page 14: JavaScript LevelUp by Lee Brandt

Asynchronicity Callbacks

function callbackFunction(){

// stuff and such

}

function asynchronousThing(cb){

// asynchronous stuff

cb();

}

asynchronousThing(callbackFunction);

Page 15: JavaScript LevelUp by Lee Brandt

Asynchronicity Promisesfunction asynchronousThing(){

var promise = $q.defer();

if(successful){

promise.resolve(results);

}else{

promise.reject();

}

return promise;

}

Page 16: JavaScript LevelUp by Lee Brandt

Asynchronicity Promises

asynchronousThing()

.progress(function(status){})

.then(function(result){})

.success(function(result){})

.error(function(err){})

.catch(function(err){})

.finally(function(){});

Page 17: JavaScript LevelUp by Lee Brandt

Quirks JavaScript

this != this

unless you bind(), call(), or apply()

function clickHandler(e){

console.log(this === e.currentTarget);

}

(function clickHandler(e){

console.log(this === myThisContext);

}).bind(myThisContext);

Page 18: JavaScript LevelUp by Lee Brandt

Hoisting

console.log(foo); // undefined

console.log(bar); // undefined

var foo = ‘baz';

var bar = 'bam';

console.log(foo); // baz

console.log(bar); // bam

Page 19: JavaScript LevelUp by Lee Brandt

Hoistingfoo(); // I am in foo

bar(); // undefined

// function declaration

function foo(){

console.log('I am in foo');

}

// function expression

var bar = function bar(){

console.log('I am in bar');

}

Page 20: JavaScript LevelUp by Lee Brandt

Tips & Tricks

Use ‘use strict’

use === instead of ==

use a module pattern

truthy and falsy

Bookmark MDN

Namespacing

Page 21: JavaScript LevelUp by Lee Brandt

Revealing Module

function myModule(){

var myPrivateThing = function(){

// does something

};

return {

MyPublicThing: myPrivateThing

};

}

Page 22: JavaScript LevelUp by Lee Brandt

Useful LinksMozilla Developer Network

- https://developer.mozilla.org

ECMA Website

- http://www.ecma-international.org/ecma-262/5.1/

jsbin.com

jsfiddle.com

plnkr.co

codepen.io

Page 23: JavaScript LevelUp by Lee Brandt

Дякую!