ECMAScript 6
WebComponents
Cache/Storage
Service Workers
Web APIs
Progressive Web Apps
ECMAScript 6
WebComponents
Cache/Storage
Service Workers
Web APIs
Progressive Web Apps
class Shape {
constructor(x, y) {
this.move(x, y);
}
move(x, y) {
this.x = x;
this.y = y;
}
}
class Rectangle extends Shape {
constructor(x, y, width, height) {
super(x, y);
this.width = width;
this.height = height;
}
}
var Shape = function (x, y) {
this.move(x, y);
};
Shape.prototype.move = function (x, y) {
this.x = x;
this.y = y;
};
var Rectangle = function (width, height) {
this.width = width;
this.height = height;
};
Rectangle.prototype = new Shape(1, 5);
var smallRectangle = new Rectangle(10, 20);
class Classroom {
constructor() {
this.students = [
{name: 'Joe', isMyFriend: false},
{name: 'Eric', isMyFriend: true}];
this.anyFriendThere = false;
}
checkIfAnyFriendThere() {
this.students.forEach((student) => {
if (student.isMyFriend) {
this.anyFriendThere = true;
}
});
}
}
var classroom = {
students: [
{name: 'Joe', isMyFriend: false},
{name: 'Eric', isMyFriend: true}],
anyFriendThere: false,
checkIfAnyFriendThere: function () {
this.students.forEach(function (student) {
if (student.isMyFriend) {
this.anyFriendThere = true;
}
}.bind(this)); // binds this to the function context
}
};
let sayHello = function (name) {
return new Promise(function (resolve, reject) {
setTimeout(() => resolve(`Hello ${name}`), 1000);
});
};
sayHello('Joe')
.then((helloToJoe) => {
console.log(`Someone says: ${helloToJoe}`);
return sayHello('Eric');
})
.then((helloToEric) => {
console.log(`Someone says: ${helloToEric}`);
});
var sayHello = function (name, onSuccess) {
setTimeout(function () {
onSuccess('Hello ' + name);
}, 1000);
};
sayHello('Joe', function (helloToJoe) {
console.log('Someone says: ' + helloToJoe);
sayHello('Eric', function (helloToEric) {
console.log('Someone says: ' + helloToEric);
});
});
Code examples from http://es6-features.org/ Code examples from http://es6-features.org/
// greeter.js
export function sayHelloTo(name) {
return `Hello ${name}`;
}
// my-code.js
import {sayHelloTo} from './greeter';
console.log(sayHelloTo('Joe'));
// greeter.js
var myNamespace = {};
myNamespace.sayHelloTo = function (name) {
return `Hello ${name}`;
};
// my-code.js
console.log(myNamespace.sayHelloTo('Joe'));
import {Input, Component} from '@angular/core';
@Component({
selector: 'hello',
template: '<h1>Hello {{name}}!</h1>'
} as Component)
export class HelloComponent {
@Input()
name: string;
}
ECMAScript 6
WebComponents
Cache/Storage
Service Workers
Web APIs
Progressive Web Apps
<template id="my-template">
<div>
<p>This is my template</p>
</div>
</template>
var template = document.querySelector('#my-template'),
clone = document.importNode(template.content, true);
document.body.appendChild(clone);
Demo </>
<template id="my-template">
<div><p>This is my template</p></div>
</template>
<div id="john">
<p>John</p>
</div>
<div id="eric">
<p>Eric</p>
</div>
var template = document.querySelector('#my-template'),
host = document.querySelector('#eric'),
root = host.attachShadow({mode: 'closed'}),
clone = document.importNode(template.content, true);
root.appendChild(clone);
Demo </>
<my-template></my-template>
<my-template></my-template>
<my-template></my-template>
<template id="my-template">
<div><p>This is my template</p></div>
</template>
var myTemplateHtmlElement = Object.create(HTMLElement.prototype);
myTemplateHtmlElement.createdCallback = function () {
var template = document.querySelector('#my-template'),
clone = document.importNode(template.content, true),
root = this.attachShadow({mode: 'closed'});
root.appendChild(clone);
};
document.registerElement("my-template",
{prototype: myTemplateHtmlElement});
Demo </>
<head>
<link rel="import" href="my-template/my-template.html">
</head>
<body>
<my-template></my-template>
<my-template></my-template>
<my-template></my-template>
</body>
Demo </>
<link rel="import"
href="https://polygit.org/components/polymer/polymer.html">
<dom-module id="my-template">
<template>
<div><p>This is my template</p></div>
</template>
<script>
Polymer({
is: "my-template"
});
</script>
</dom-module>
Demo </>
ECMAScript 6
WebComponents
Cache/Storage
Service Workers
Web APIs
Progressive Web Apps
ECMAScript 6
WebComponents
Cache/Storage
Service Workers
Web APIs
Progressive Web Apps
var worker = new Worker('task.js');
worker.addEventListener('message', function(e) {
console.log('Worker said: ', e.data);
}, false);
worker.addEventListener('error', function(e){
console.log('ERROR: Line', e.lineno, 'in',
e.filename, ':', e.message);
}, false);
worker.postMessage('Hello World'); // Send data to our worker.
self.addEventListener('message', function(e) {
self.postMessage(e.data);
}, false);
Code from https://www.html5rocks.com/en/tutorials/workers/basics/
(on first installation)
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js')
.then(function(registration) {
// Registration was successful
console.log('Registration successful with scope: ',
registration.scope);
}).catch(function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
}
self.addEventListener('install', function(event) {
// Perform install steps
});
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open('mysite-static-v3').then(function(cache) {
return cache.addAll([
'/css/whatever-v3.css',
'/css/imgs/sprites-v6.png',
'/css/fonts/whatever-v8.woff',
'/js/all-min-v4.js'
// etc
]);
})
);
});
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open('mygame-core-v1').then(function(cache) {
cache.addAll(
// levels 11-20
);
return cache.addAll(
// core assets & levels 1-10
);
})
);
});
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.filter(function(cacheName) {
// Return true if you want to remove this cache,
// but remember that caches are shared across
// the whole origin
}).map(function(cacheName) {
return caches.delete(cacheName);
})
);
})
);
});
document.querySelector('.cache-article')
.addEventListener('click', function(event) {
event.preventDefault();
var id = this.dataset.articleId;
caches.open('mysite-article-' + id)
.then(function(cache) {
fetch('/get-article-urls?id=' + id)
.then(function(response) {
return response.json();
}).then(function(urls) {
cache.addAll(urls);
});
});
});
ECMAScript 6
WebComponents
Cache/Storage
Service Workers
Web APIs
Progressive Web Apps
Instant Loading
Fast
Add to Home Screen
Secure
Push Notifications
Responsive
{
"short_name": "AirHorner",
"name": "Kinlan's AirHorner of Infamy",
"icons": [{
"src": "launcher-icon-1x.png",
"type": "image/png",
"sizes": "48x48"
},
...
{
"src": "launcher-icon-4x.png",
"type": "image/png",
"sizes": "192x192"
}],
"start_url": "index.html?launcher=true"
}
Multiplexed downloads over a single connection
Allows the server to preemptively send resources to the browser