Precedence
47 |-
48 |
- Lambda 49 |
- Fat Arrow Function 50 |
Defacto Standards
52 |-
53 |
- CoffeeScript 54 |
- Fat Arrow Functions 55 |
- Skinny Arrow Functions 56 |
- ES6 uses Fat Arrow syntax, calls it Arrow Function 57 |
├── .gitignore
├── .idea
├── .name
├── encodings.xml
├── es6-fm.iml
├── misc.xml
├── modules.xml
├── scopes
│ └── scope_settings.xml
├── vcs.xml
└── workspace.xml
├── README.md
├── app.yaml
├── arrow.html
├── classes.html
├── collections.html
├── config.rb
├── defaultparams.html
├── destructure.html
├── generators.html
├── history.html
├── images
├── adobe.png
├── barchart.png
├── blueprints.jpg
├── bucketes6.png
├── bucketharmony.png
├── bucketnext.png
├── buckets.png
├── celebration1.gif
├── chart.png
├── chrome-logo-tiny.png
├── cppvsjava.gif
├── dndgeek.jpg
├── doublestamp.gif
├── eatajax.jpg
├── es6.png
├── firefoxsuperhero.jpg
├── frontendmasters-frosty.png
├── frontendmasters-frosty2.png
├── frontendmasters_logo.png
├── google_developers_icon_128.png
├── google_developers_logo.png
├── google_developers_logo_tiny.png
├── google_developers_logo_white.png
├── io2012_logo.png
├── io2013_logo.png
├── jumpcanyon.jpg
├── lensesforcardboard.jpg
├── mozilla.png
├── mozillafoundation.png
├── mrafternoont.jpg
├── mrt.jpeg
├── ms.png
├── netscape.png
├── norrisgif.gif
├── nutshell.gif
├── ohwow.gif
├── opera.png
├── periodic.png
├── ptcfoo1.png
├── ptcfoo2.png
├── ptcfoo3.png
├── ptcfoon.png
├── ptcimproper.png
├── ptcproper.png
├── pyramid.png
├── sky.jpg
├── tc39.png
├── teamfight.gif
├── teamfight.jpg
├── teamfight.png
├── teamfightmaxresdefault.jpg
├── teamfightwinner.png
├── uofi.jpg
├── wtf1.jpg
├── yahoo.png
└── ysoasync.jpg
├── index.html
├── js
├── hammer.js
├── modernizr.custom.45394.js
├── order.js
├── polyfills
│ ├── classList.min.js
│ ├── dataset.min.js
│ └── history.min.js
├── prettify
│ ├── lang-apollo.js
│ ├── lang-clj.js
│ ├── lang-css.js
│ ├── lang-go.js
│ ├── lang-hs.js
│ ├── lang-lisp.js
│ ├── lang-lua.js
│ ├── lang-ml.js
│ ├── lang-n.js
│ ├── lang-proto.js
│ ├── lang-scala.js
│ ├── lang-sql.js
│ ├── lang-tex.js
│ ├── lang-vb.js
│ ├── lang-vhdl.js
│ ├── lang-wiki.js
│ ├── lang-xq.js
│ ├── lang-yaml.js
│ ├── prettify.css
│ └── prettify.js
├── require-1.0.8.min.js
├── slide-controller.js
├── slide-deck.js
└── slides.js
├── letconst.html
├── modules.html
├── promises.html
├── ptc.html
├── rest.html
├── samples
├── MonsterClass.js
├── let.js
└── temp
│ └── let.tcr.js
├── scripts
└── md
│ ├── README.md
│ ├── base.html
│ ├── render.py
│ └── slides.md
├── serve.sh
├── slide_config.js
├── spread.html
├── temp.html
├── template.html
└── theme
├── css
├── default.css
├── fe.css
└── phone.css
└── scss
├── _base.scss
├── default.scss
├── fe.scss
└── phone.scss
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled source #
2 | ###################
3 | *.com
4 | *.class
5 | *.dll
6 | *.exe
7 | *.o
8 | *.so
9 | *.pyc
10 | *.min.css
11 | #*.min.js
12 | .sass-cache/*
13 |
14 | # Packages #
15 | ############
16 | # it's better to unpack these files and commit the raw source
17 | # git has its own built in compression methods
18 | *.7z
19 | *.dmg
20 | *.gz
21 | *.iso
22 | *.rar
23 | *.tar
24 | *.zip
25 |
26 | # Logs and databases #
27 | ######################
28 | *.log
29 | *.sql
30 | *.sqlite
31 |
32 | # OS generated files #
33 | ######################
34 | .DS_Store*
35 | ehthumbs.db
36 | Icon?
37 | Thumbs.db
38 | *~
39 | .idea
40 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | es6-fm
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
Precedence
47 |Defacto Standards
52 |Compared to regular functions
76 |77 | var fn1 = function(){return 2;}; 78 | var fn2 = () => 2; 79 |80 |
81 |
Parenthesis-Parameter Rules
96 |
97 | var x;
98 | x = () => {}; //No parameters, MUST HAVE PARENS
99 | x = (val) => {}; //One parameter w/ parens, OPTIONAL
100 | x = val => {}; //One parameter w/o parens, OPTIONAL
101 | x = (y, z) => {}; //Two or more parameters, MUST HAVE PARENS
102 | x = y, z => {}; //Syntax Error: must wrap with parens when using multiple params
103 |
104 | 105 |
Method Body Declarations
118 |119 | var square; 120 | square = x => x * x; //Body w/o braces 121 | square = x => { return x * x}; //Body w/ braces 122 |123 |
124 |
Use them in place of anonymous functions
138 |139 | let nums = [1, 2, 3]; 140 | let res = nums.map( n => n * n ); 141 | console.log(res); //Logs [1, 4, 9] 142 |143 |
Return an object literal, wrap in parens
152 |153 | let key_maker = val => ({key: val}); 154 | console.log(key_maker(100)); //Logs {key: 100} 155 |156 |
The REAL benefit: lexical binding of 'this'
165 |168 | var Widget = { 169 | init: function() { 170 | document.addEventListener("click", function(event) { 171 | this.doSomething(event.type); // Why does this error? 172 | }, false); 173 | }, 174 | 175 | doSomething: function(type) { 176 | console.log("Handling " + type + " event"); 177 | } 178 | }; 179 | Widget.init(); 180 |181 |
The REAL benefit: lexical binding of 'this'
216 |219 | var Widget = { 220 | init: function(){ 221 | document.addEventListener("click", (event) => { 222 | this.doSomething(event.type); 223 | }, false); 224 | }, 225 | 226 | doSomething: function(type){ 227 | console.log("Handling " + type + " event"); 228 | } 229 | }; 230 | Widget.init(); 231 |232 |
Additional Info about '() => {}'
244 |247 | typeof ()=>{}; //'function' 248 |249 |
253 | Object.getPrototypeOf(()=>{}); //Function.prototype 254 |255 |
258 | var Foo = function(){}; 259 | var Bar = () => {}; 260 | new Foo(); 261 | new Bar(); //Bar is not a contructor 262 |263 |
Additional Info about '() => {}'
275 |278 | function Widget(){ 279 | this.id = 123; 280 | this.log = () => { 281 | console.log('Widget Log', this.id); 282 | } 283 | } 284 | var pseudoWidget = { 285 | id: 456 286 | }; 287 | new Widget().log.call(pseudoWidget); //What Logs? 288 |289 |
Still Use Functions
302 |305 | var Widget = { 306 | init: function(){ 307 | document.addEventListener("click", (event) => { 308 | this.doSomething(event.type); 309 | }, false); 310 | }, 311 | 312 | doSomething: function(type){ 313 | console.log("Handling " + type + " event"); 314 | } 315 | }; 316 | Widget.init(); 317 |318 |
47 |
64 |
84 |
96 |
99 | //FUNCTIONALLY SAME 100 | function Foo(){ 101 | //.... 102 | } 103 | 104 | class Foo{ 105 | //.... 106 | } 107 |108 |
120 | Constructor 121 |
122 |123 | class Animal{ 124 | 125 | constructor(name){ 126 | this.name = name; 127 | } 128 | 129 | } 130 |131 |
140 | 141 | Private Properties 142 | 143 |
144 |145 | const monsterHealth = Symbol(); 146 | 147 | class Monster{ 148 | constructor(name, health){ 149 | this.name = name; 150 | this[monsterHealth] = health; 151 | } 152 | } 153 |154 |
166 | Get Properties 167 |
168 |169 | const monsterHealth = Symbol(); 170 | 171 | class Monster{ 172 | constructor(name, health){ 173 | this.name = name; 174 | this[monsterHealth] = health; 175 | } 176 | 177 | get isAlive(){ 178 | return this[monsterHealth] > 0; 179 | } 180 | } 181 |182 |
201 | Set Properties 202 |
203 |204 | //.... 205 | class Monster{ 206 | constructor(name, health){ 207 | this.name = name; 208 | this[monsterHealth] = health; 209 | } 210 | //... 211 | set health(val){ 212 | if(val < 0){ 213 | throw new Error('Health must be positive number'); 214 | } 215 | this[monsterHealth] = val; 216 | } 217 | } 218 |219 |
238 | Prototype Methods 239 |
240 |241 | //.... 242 | class Monster{ 243 | constructor(name, health){ 244 | this.name = name; 245 | this[monsterHealth] = health; 246 | } 247 | //... 248 | attack(target){ 249 | console.log(this.name + ' attacks ' + target.name); 250 | } 251 | } 252 |253 |
274 | Class Properties 275 |
276 |277 | class Monster{ 278 | constructor(name, health){ 279 | this.name = name; 280 | this[monsterHealth] = health; 281 | Monster.allMonsters.push(this); 282 | } 283 | //.... 284 | } 285 | Monster.allMonsters = []; 286 |287 |
296 | Extend Classes 297 |
298 |299 | class Monster{ 300 | constructor(name, health){ 301 | this.name = name; 302 | this[monsterHealth] = health; 303 | } 304 | //.... 305 | } 306 | class Godzilla extends Monster{ 307 | constructor(){ 308 | super('Godzilla', 10000); 309 | } 310 | } 311 |312 |
337 | Extend Classes via Expression 338 |
339 |340 | class MySocket extends getClass() { 341 | //.... 342 | } 343 | 344 | function getClass(){ 345 | if(isIE()){ 346 | return IEWebSocketImpl; 347 | } 348 | return WebSocket; 349 | 350 | function isIE(){ 351 | return false; 352 | } 353 | } 354 |355 |
364 | class Monster{ 365 | constructor(name, health){ 366 | this.name = name; 367 | this[monsterHealth] = health; 368 | } 369 | attack(target){...} 370 | } 371 | class Godzilla extends Monster{ 372 | constructor(){ 373 | super('Godzilla', 10000); 374 | } 375 | attack(target){ 376 | super(target); //Currently THIS 377 | super.attack(target); //Could Change to THIS 378 | } 379 | } 380 |381 |
390 |
393 | new Bar(); //runtime error 394 | 395 | class Bar{} 396 |397 |
406 |
409 | 410 | constructor(..args){ 411 | super(...args); 412 | } 413 | 414 |415 |
424 |
47 |
65 |
82 |
88 | function test(a) { 89 | a = a || getSomeDefaultValue(); //DATAPROOFING 90 | /*Your code*/ 91 | } 92 |93 |
97 | function test(a) { 98 | a = a ? a : getSomeDefaultValue(); //DATAPROOFING 99 | /*Your code*/ 100 | } 101 |102 |
113 |
116 | function sayHello(name = "World"){ 117 | console.log("Hello " + name + "!"); 118 | } 119 | 120 | sayHello("Dt Dew"); //Hello Dt Dew! 121 | sayHello(""); //Hello ! 122 | sayHello(); //Hello World! 123 | sayHello(undefined);//Hello World! 124 |125 |
140 |
156 |
161 | function getRand(){ 162 | return Math.ceil(Math.random() * 10000000) + new Date().getTime(); 163 | } 164 | 165 | function myFunction(id=getRand()){ 166 | console.log("My ID: "+id); 167 | } 168 | 169 | myFunction(); //Logs random number 170 | myFunction(1); //Logs 1 171 |172 |
183 |
188 | function die(){ throw "x"} 189 | 190 | function test(a = die()){ 191 | console.log("Didn't die"); 192 | } 193 | 194 | test(); // throws an error 195 |196 |
208 |
213 | var x = "INIT"; 214 | 215 | function test(a = x) { 216 | var x; 217 | return a; 218 | } 219 | 220 | console.log( test() ); //undefined 221 |222 |
234 |
239 | function test(a = 1, b){ //This is OK 240 | //Your Code 241 | } 242 |243 |
255 |
260 | function f ( ...rest=100 ) { //SyntaxError 261 | //Your Code 262 | } 263 |264 |
276 |
281 | function test (a = 1, b = 2, c = 3){ 282 | console.log(arguments.length); 283 | } 284 | 285 | test(); // 0 286 | test(1); // 1 287 | test(1,2,3,4,5); // 5 288 |289 |
As we talk about the new features
149 |We want everyone to practice writing the new syntax
166 |185 | History of JavaScript 186 |
187 |188 |
Features
189 | 190 |47 |
62 |
80 |
116 |
135 | Promise Constructor 136 |137 | 138 | 139 | 140 | 141 | 142 |
151 | var promise = new Promise(function(resolve, reject) { 152 | // do a thing, possibly async, then… 153 | 154 | if (/* everything turned out fine */) { 155 | resolve("Stuff worked!"); 156 | } 157 | else { 158 | reject(Error("It broke")); 159 | } 160 | }); 161 | 162 | return promise; //Give This To Someone 163 |164 |
182 | var promise = new Promise(function(resolve, reject) { 183 | // do a thing, possibly async, then… 184 | 185 | //WHAT COULD WE DO HERE? 186 | }); 187 |188 |
213 | var promise = new Promise(function(resolve, reject) { 214 | // do a thing, possibly async, then… 215 | 216 | }); 217 |218 |
233 | Promise Instance 234 |235 | 236 | 237 | 238 | 239 | 240 |
282 | var promise = new Promise(function(resolve, reject){ 283 | //Do Something 284 | if(somthingWorked()){ 285 | resolve("Stuff worked!"); 286 | } else { 287 | reject("It broke"); 288 | } 289 | }); 290 | 291 | promise.then(function(result) { 292 | console.log(result); // "Stuff worked!" 293 | }, function(err) { 294 | console.log(err); // Error: "It broke" 295 | }); 296 |297 | 298 |
314 | //Made My Own GET method 315 | 316 | function get(url){ 317 | return new Promise(function(resolve, reject){ 318 | $.get(url, function(data){ 319 | resolve(data); 320 | }) 321 | .fail(function(){ 322 | reject(); 323 | }); 324 | }); 325 | } 326 | 327 |328 | 329 |
345 | 346 | get('users.all').then(function(users){ 347 | myController.users = users; 348 | }, function(){ 349 | delete myController.users; 350 | }); 351 | 352 | 353 |354 |
370 | //.catch instead of second handler in .then 371 | get('users.all') 372 | .then(function(users){ 373 | myController.users = users; 374 | }) 375 | .catch(function(){ 376 | delete myController.users; 377 | }); 378 | 379 | 380 |381 |
397 | 398 | var usersPromise = get('users.all'); 399 | var postsPromise = get('posts.everyone'); 400 | 401 | //Wait until BOTH are settled 402 | Promise.all([usersPromise, postsPromise]) 403 | .then(function(results){ 404 | myController.users = results[0]; 405 | myController.posts = results[1]; 406 | }, function(){ 407 | delete myController.users; 408 | delete myController.posts; 409 | }); 410 | 411 |412 |
431 |
432 | get('users.all').then(function(usersString){
433 | return JSON.parse(usersString);
434 | }).then(function(users){
435 | myController.users = users;
436 | });
437 |
438 |
439 |
440 |
460 | //Even More Compact
461 | get('users.all').then(JSON.parse).then(function(users){
462 | myController.users = users;
463 | });
464 |
465 |
466 | 480 | Static Promise Methods 481 |482 | 483 | 484 | 485 | 486 | 487 |