59
Lecture 2: ES6/ES2015 and Beyonds with the Best Practices Kobkrit Viriyayudhakorn, Ph.D. CEO of iApp Technology Limited. [email protected] http://www.kobkrit.com

Lecture 2: ES6 / ES2015 Slide

Embed Size (px)

Citation preview

Page 1: Lecture 2: ES6 / ES2015 Slide

Lecture 2: ES6/ES2015 and Beyonds with the Best Practices

Kobkrit Viriyayudhakorn, Ph.D. CEO of iApp Technology Limited.

[email protected] http://www.kobkrit.com

Page 2: Lecture 2: ES6 / ES2015 Slide

What we will learn today?

• A bit on JavaScript History

• Learn how to program in the modern JavaScript i.e., ECMAScript 2015 (Version 6) / ECMAScript 6 / ES2015 / ES6 complied by BabelJS

• Program with the best practices (Airbnb coding style) and style checking tools by ESLint

Page 3: Lecture 2: ES6 / ES2015 Slide

Short History• Want to added support for Java applets more

accessible to non-Java programmers in Netscape.

• Developed by Brendan Eich of Netscape

• December 1995

• Mocha => LiveScript => JavaScript

• Popular!

• Microsoft release JScript

• NetScape submit to ECMA for standardize

Page 4: Lecture 2: ES6 / ES2015 Slide

ECMAScript• Script-Language

Specification

• Standardized by Ecma International in ECMA-262 and ISO/IEC 16262, based on JavaScript

• First Appeared June 1997, 19 year ago.

Page 5: Lecture 2: ES6 / ES2015 Slide

Significant Development• V8 JavaScript engine (2008). To compete with Internet

Explorer on JavaScript benchmark, Google developed v8 that can compile JavaScript to Native Machine code. It is very fast.

• NodeJS + NPM (2009): Using V8 to make server-side JavaScript Web Application.

• MongoDB (2009): No SQL database based on V8 engine.

• React (2013): JavaScript Front-end Web framework for creating Interactive UI.

• React-Native (2015): Enable to Mobile App Development

Page 6: Lecture 2: ES6 / ES2015 Slide

https://www.youtube.com/watch?v=8ybquJSjZHg

Page 7: Lecture 2: ES6 / ES2015 Slide

HomeBrew Installation

• Open Terminal (Click on Find icon on the top right of the screen)

• Type “Terminal”

• Enter $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Page 8: Lecture 2: ES6 / ES2015 Slide

Install React-Native in Mac for iOS Development.

Page 9: Lecture 2: ES6 / ES2015 Slide

React-Native-Cli Command

Page 10: Lecture 2: ES6 / ES2015 Slide

Atom IDE

https://atom.io/

Page 11: Lecture 2: ES6 / ES2015 Slide

Setup the Project• Download atom IDE and install

• $ cd workingFolder

• $ react-native init l2es6

• $ cd l2es6

• $ react-native run-ios

• $ atom .

Page 12: Lecture 2: ES6 / ES2015 Slide

Making a new JS file

Right click on l2es6, Select New File, Type down a new file with JS extension

Page 13: Lecture 2: ES6 / ES2015 Slide

Running JS Script• Open Terminal (Open

Spotlight  , Type Terminal)

• Change Directory to working directory ($ cd {working dir})

• $ node {filename.js}

Page 14: Lecture 2: ES6 / ES2015 Slide

Let’s Start with ES6

Page 15: Lecture 2: ES6 / ES2015 Slide

ES6 Basic• /* Comment */

• // Comment

• console.log(‘print out’);

• Syntax mostly derived from C language

• if (true){}

• while (true){}

• do{…} while(true);

• for(init ; condition; incr) {}

Page 16: Lecture 2: ES6 / ES2015 Slide

Primitive Types• Directly Store Value

• string

• number

• boolean

• null

• undefined

Page 17: Lecture 2: ES6 / ES2015 Slide

Primitive Type #1: String, Number, and Boolean

Page 18: Lecture 2: ES6 / ES2015 Slide

Primitive Type #2: Null, Undefined, and Reference by Value

Page 19: Lecture 2: ES6 / ES2015 Slide

Exercise 1

Page 20: Lecture 2: ES6 / ES2015 Slide

Complex Types• Reference of its value

• object

• array

• function

Page 21: Lecture 2: ES6 / ES2015 Slide

Complex Type #1: Object & Array

Page 22: Lecture 2: ES6 / ES2015 Slide

Complex Type #2: Function and Pass by Reference

Page 23: Lecture 2: ES6 / ES2015 Slide

Exercise 2

Page 24: Lecture 2: ES6 / ES2015 Slide

Declaration (Var, Let and Const)

• Var, Let, Const = Making Variable Declaration

• Const = Constant Declaration, Can’t Reassign.

• Use with reference that never change.

• Block-Level Scope

• Safer (If reassignment happen, it will throw the errors)

Page 25: Lecture 2: ES6 / ES2015 Slide

Declaration (Var, Let and Const)

• Let = Variable Declaration, Can Reassign.

• Block-Level Scope

• Var = Variable Declaration, Can Reassign

• Global-Level Scope

• Old JavaScript

• Dangerous. Don’t use it.

Page 26: Lecture 2: ES6 / ES2015 Slide

Declaration #1: Global Scope, Block Scope and Constant

Page 27: Lecture 2: ES6 / ES2015 Slide

Declaration #2: General Use Case in For loop

Page 28: Lecture 2: ES6 / ES2015 Slide

Exercise 3

Page 29: Lecture 2: ES6 / ES2015 Slide

Functions• Function in JavaScript are objects, which can use

as arguments.

• Function can be invoked.

Page 30: Lecture 2: ES6 / ES2015 Slide

Function #1: Declaration, Invocation

Page 31: Lecture 2: ES6 / ES2015 Slide

Function #2: ES6 Feature - Declaration

Page 32: Lecture 2: ES6 / ES2015 Slide

Exercise 4

Page 33: Lecture 2: ES6 / ES2015 Slide

Function #3: Anonymous Function

Page 34: Lecture 2: ES6 / ES2015 Slide

Function #4: Recursive

Page 35: Lecture 2: ES6 / ES2015 Slide

Function #5: Callback

Page 36: Lecture 2: ES6 / ES2015 Slide

Exercise 5

Page 37: Lecture 2: ES6 / ES2015 Slide

Function #6: Exceptions

Page 38: Lecture 2: ES6 / ES2015 Slide

Function #7: ES6 Feature - Rest Params (Spreads …), Default Params

Page 39: Lecture 2: ES6 / ES2015 Slide

Function #8: ES6 Feature - Named Parameters

Page 40: Lecture 2: ES6 / ES2015 Slide

Exercise 6

Page 41: Lecture 2: ES6 / ES2015 Slide

Function’s Airbnb Style Guides

Style Guide #1. Use function declarations instead of function expressions.

Page 42: Lecture 2: ES6 / ES2015 Slide

Style Guide #2. Wrap immediately invoked function expressions in parentheses.

Why? An immediately invoked function expression is a single unit - wrapping both it, and its invocation parens, in parens, cleanly expresses

this.

Page 43: Lecture 2: ES6 / ES2015 Slide

Style Guide #3. Never declare a function in a non-function block (if, while, etc.)

Page 44: Lecture 2: ES6 / ES2015 Slide

Style Guide #4. Never use arguments, use … instead.

Page 45: Lecture 2: ES6 / ES2015 Slide

Style Guide #5. Use parameter syntax rather than mutating function arguments.

Page 46: Lecture 2: ES6 / ES2015 Slide

Style Guide #6. Avoid side effects with default parameters

Page 47: Lecture 2: ES6 / ES2015 Slide

Style Guide #7: Always put default parameters last.

Page 48: Lecture 2: ES6 / ES2015 Slide

Style Guide #8: Use spread operator … to call variadic function.

Page 49: Lecture 2: ES6 / ES2015 Slide

Style Guide #8: Use Arrow Functions when passing an anonymous function.

Page 50: Lecture 2: ES6 / ES2015 Slide

Style Guide #9: Omit braces and use the implicit return, if the function body consists of a single expressions.

Page 51: Lecture 2: ES6 / ES2015 Slide

Style Guide #10: If your function takes a single argument and doesn’t use braces, omit the parentheses. Otherwise, always

include parentheses around arguments.

Page 52: Lecture 2: ES6 / ES2015 Slide

Object• Object is a dictionary data structure.

• Map between Key => Value

Page 53: Lecture 2: ES6 / ES2015 Slide

Object #1: Declaration, Reference

Page 54: Lecture 2: ES6 / ES2015 Slide

Object #2: Assignment, Retrieval, Re-Assignment

Page 55: Lecture 2: ES6 / ES2015 Slide

Object #3: Deletion, Keys, Size, Loops

Page 56: Lecture 2: ES6 / ES2015 Slide

Exercise 7• Complete the following code that can change digit

to reading word.For example, 12.3 => “one two dot three”

Starting code

Page 57: Lecture 2: ES6 / ES2015 Slide

HomeWork 1(1) Write down a function that sum every element in array. E.g. sumArray([12,3,4,1,2,3]) = 25

Page 58: Lecture 2: ES6 / ES2015 Slide

HomeWork 2(2) Write function that count word size case-insensitively.

Input: "Hello world hello hello earth earth" (Not limited to these word, it can be any words)

Output: Object{hello : 3, world : 1, earth : 2 }

Page 59: Lecture 2: ES6 / ES2015 Slide

Q/A