UP CSI DevCap 1819B
Regional Director of Engineering
Freelancer.com
A Brief History of JavaScript
1995
JavaScript is invented
by Brendan Eich, Netscape Communications Corporation
in 10 days
originally called Mocha
A Brief History of JavaScript
JavaScript is handed over to ECMA
Given to official name ECMAScript (ES)
Standard ECMA-262
1996
1995
A Brief History of JavaScript
ES1/ES2 released
ES1 was just the original language description
ES2 was just an editorial change for ISO/IEC 16262
1996
1995
1997
1998
A Brief History of JavaScript
ES3 released
regular expressions
try/catch
formatting for numeric output
1999
1996
1995
1997
1998
A Brief History of JavaScript
ES5 released
JSON parsing/serialization
Array prototype methods
methods for listing object properties
dangling commas
"strict mode"
1999
2009
1997
1998
1996
1995
A Brief History of JavaScript
renamed from ES6 to ES2015
first of the new yearly June release schedule
classes and modules
iterators, generators and for/of loops
arrow functions
collections (maps/sets)
promises
1999
2009
1997
1998
1996
1995
ES6/ES2015 released
2015
A Brief History of JavaScript
ES2016: exponentiation operator (**)
ES2016: Array.prototype.includes
ES2017: async/await
1999
2009
1997
1998
1996
1995
ES2016/ES2017 released
2015
2016
2017
A Brief History of JavaScript
new regular expression features
rest/spread parameters
1999
2009
1997
1998
1996
1995
ES2018 released
2015
2016
2017
2018
A Brief History of JavaScript
Basic Syntax
<script>
...
</script>
<script src="/path/to/source.js"></script>Basic Syntax
// This is a single line comment
/*
This is a multi line comment.
It can have as many lines as you need.
*/Basic Syntax
var myVariable = "This is the value.";
| Type | Description | Example |
|---|---|---|
| String | A sequence of characters delimited by quotes. | var x = "string"; |
| Number | Any integer or floating point number. | var x = 100; |
| Boolean | The values true or false. | var x = true; |
| Array | A structure that allows you to store multiple values in one single reference. | var x = [1, 2, 3]; |
| Object | A collection that allows you to store key/value pairs. | var x = { a: 1, b: "test" }; |
Basic Syntax
| Type | Description | Symbol | Example |
|---|---|---|---|
| Addition | Used for numeric addition and string concatenation. | + | 6 + 9; "Hello " + "world!"; |
| Subtraction, Multiplication, Division | These do what you'd expect them to do in basic math. | -, *, / | 9 - 3; 8 * 2; 9 / 3; |
| Assignment | You've seen this already: it assigns a value to a variable. | = | var x = "value"; |
| Equality | Does a test to see if two values are equal to one another and returns a true/false (Boolean) result. | === | var x = 3; x === 4; // returns false |
| Not | Returns the logically opposite value of what it precedes. | ! | var x = 3; !(x === 4); // returns true |
| Inequality | Tests whether two values are not equal. | !== | var x = 3; x !== 4; // returns true |
Basic Syntax
var iceCream = 'chocolate';
if (iceCream === 'chocolate') {
alert('Yay, I love chocolate ice cream!');
} else {
alert('Awwww, but chocolate is my favorite...');
}Basic Syntax
// basic counter using a for loop
for (x = 1; x <= 10; x++) {
console.log(x);
}Basic Syntax
// Same counter using a while
var x = 1;
while (x <= 10) {
console.log(x);
x = x + 1;
}Basic Syntax
alert('hello!');function multiply(num1,num2) {
var result = num1 * num2;
return result;
}
multiply(4, 7);
multiply(20, 20);
The Event Loop
The Event Loop
The Event Loop
Main Loop
The Event Loop
JavaScript Task Loop
The Event Loop
Rendering Steps
The Event Loop
Animation Callbacks
The Event Loop
Microtasks?
The Event Loop
Jake Archibald @ JSConf Asia 2018
New Features
function f() {
{
let x;
{
// this is ok since it's a block scoped name
const x = "sneaky";
// error, was just defined with `const` above
x = "foo";
}
// this is ok since it was declared with `let`
x = "bar";
// error, already declared above in this block
let x = "inner";
}
}New Features
// Expression bodies
var increment = x => x + 1;
var add = (x, y) => x + y;
increment(1); // returns 2
add(1, 2); returns 3New Features
// Spread example
function f(x, y, z) {
return x + y + z;
}
// Pass each elem of array as argument
f(...[1,2,3]) == 6
// Rest example
function f(x, ...y) {
// y is an Array
return x * y.length;
}
f(3, "hello", true) == 6New Features
function timeout(duration = 0) {
return new Promise((resolve, reject) => {
setTimeout(resolve, duration);
});
}
var p = timeout(1000).then(() => {
return timeout(2000);
}).then(() => {
throw new Error("hmm");
}).catch(err => {
return Promise.all([timeout(100), timeout(200)]);
});New Features
// lib/math.js
export function sum(x, y) {
return x + y;
}
export var pi = 3.141593;
// app.js
import * as math from "lib/math";
console.log("2π = " + math.sum(math.pi, math.pi));
New Features
class Rectangle {
constructor(height, width) {
this.h = height;
this.w = width;
}
area() {
return this.w * this.h;
}
display() {
console.log("The height of the rectangle: ", this.h);
console.log("The width of the rectangle: ",this. w);
}
}
//creating an instance
var rectangleObj = new Rectangle(10,20);
rectangleObj.display();
console.log("The area of the rectangle: ", rectangleObj.area();New Features
class StaticExample {
static disp() {
console.log("Static Function called");
}
}
StaticExample.disp(); //invoke the static method
// Static Function calledNew Features
class Person{
constructor(name, age) {
this.name = name;
this.age = age;
}
display() {
console(this.name, " is ", this.age, " years old.");
}
}
var obj = new Person()
var isPerson = obj instanceof Person;
console.log("obj is an instance of Person " + isPerson);
// obj is an instance of Person True New Features
class Shape {
constructor(a) {
this.Area = a;
}
}
class Circle extends Shape {
disp() {
console.log("Area of the circle: "+this.Area);
}
}
var obj = new Circle(223);
obj.disp();
// Area of the circle: 223New Features
class Root {
test() {
console.log("call from parent class");
}
}
class Child extends Root {}
class Leaf extends Child {}
//indirectly inherits from Root via Child
var obj = new Leaf();
obj.test()
// call from parent classNew Features
class PrinterClass {
doPrint() {
console.log("doPrint() from Parent called… ");
}
}
class StringPrinter extends PrinterClass {
doPrint() {
console.log("doPrint() is printing a string…");
}
}
var obj = new StringPrinter();
obj.doPrint();
// doPrint() is printing a string… New Features
class PrinterClass {
doPrint() {
console.log("doPrint() from Parent called…")
}
}
class StringPrinter extends PrinterClass {
doPrint() {
super.doPrint()
console.log("doPrint() is printing a string…")
}
}
var obj = new StringPrinter()
obj.doPrint()
// doPrint() from Parent called.
// doPrint() is printing a string. New Features
fetch('https://example.com/some/url')
.then(function(response) {
// process response
})
.catch(function(err) {
// process error
});New Features
const request = new Request('https://example.com/some/url', {
headers: new Headers({
'Content-Type': 'text/plain'
})
});
fetch(request).then(function() { /* handle response */ });New Features
New Features
const request = new Request('https://example.com/some/url', {
method: 'POST',
mode: 'cors',
redirect: 'follow',
headers: new Headers({
'Content-Type': 'text/plain'
})
});
// Now use it!
fetch(request).then(function(response) { /* handle response */ });New Features
New Features
// The fetch's `then` gets a Response instance back
fetch('https://example.com/some/url')
.then(function(response) {
console.log('status: ', response.status);
});New Features
New Features
fetch('https://example.com/some/url').then(function(response) {
// Convert to JSON
return response.json();
}).then(function(j) {
// Yay, `j` is a JavaScript object
console.log(j);
});Exercises
References
UP CSI DevCap 1819B