Más contenido relacionado La actualidad más candente (20) Similar a "let ECMAScript = 6" (20) Más de The Software House (20) "let ECMAScript = 6" 2. O MNIEO MNIE
Blog:
Na co dzień PHP i JS
Inspirują mnie inne języki programowania i podejścia
http://wiktortoporek.name/blog
5. ES5 / ES5.1ES5 / ES5.1
Metody Object.*:
.create(),
.defineProperty(), ...
Natywny support JSONa
Metody w Arrayach:
.forEach(),
.map(),
.filter(), ...
12. PROSTA SKŁADNIAPROSTA SKŁADNIA
x => x * 2
function(x) {return x * 2;}
WIELE ARGUMENTÓWWIELE ARGUMENTÓW
(x, y) => x + y
function(x, y) {return x + y;}
BRAK ARGUMENTÓWBRAK ARGUMENTÓW
() => 2 + 2
function() {return 2 + 2;}
13. BARDZIEJ ZŁOŻONE CIAŁO FUNKCJIBARDZIEJ ZŁOŻONE CIAŁO FUNKCJI
(x, y) => {
console.log(x);
return x+y;
}
function(x, y) {
console.log(x);
return x+y;
}
15. /* examples/arrow/03-buggy-timer.js */
var Timer = function() {
this.secs = 0;
};
Timer.prototype.start = function() {
setInterval(function() {
this.secs++;
}, 1000);
};
var timer = new Timer();
timer.start();
17. /* examples/arrow/04-corrected-timer.js */
var Timer = function() {
this.secs = 0;
};
Timer.prototype.start = function() {
var that = this;
setInterval(function() {
that.secs++;
}, 1000);
};
var timer = new Timer();
timer.start();
39. /* examples/destruct/02-spread.js */
var head, rest;
[head, ...rest] = [1, 2, 3, 4, 5];
console.log(head); /*1*/
console.log(rest); /*[2, 3, 4, 5]*/
var array1 = [1,2,3],
array2 = [4,5,6];
/*ES5: array1.push.apply(array1, array2);*/
array1.push(...array2);
console.log(array1); //[1,2,3,4,5,6]
40. /* examples/destruct/03-func-many-params.js */
function ajax(url, method, async, headers) {
if (method === undefined) {
method = 'get';
}
if (async === undefined) {
async = true;
}
if (headers === undefined) {
headers = {};
}
console.log(method, url, async ? 'async' : 'sync');
console.log('headers:', headers);
}
/*
get http://google.com sync
headers: {}
*/
ajax('http://google.com', 'get', false);
41. /* examples/destruct/04-func-params-es6.js */
function ajax({url, method = 'get', async = true, headers = {}}) {
console.log(method, url, async ? 'async' : 'sync');
console.log('headers:', headers);
}
/*
get http://google.com sync
headers: {}
*/
ajax({url: 'http://google.com', async: false});
43. /* examples/class/01-es5.js */
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
Person.prototype.say = function(what) {
console.log(this.firstName+' '+this.lastName+' says: '+what);
};
var john = new Person('John', 'Doe');
john.say('Hello!');
44. /* examples/class/02-es6.js */
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
say(what) {
console.log(`${this.firstName} ${this.lastName} says: ${what}`);
}
}
var john = new Person('John', 'Doe');
john.say('Hello!');
45. /* examples/class/03-inheritance.js */
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
getFullName() {
return [this.firstName, this.lastName].join(' ');
}
toString() {
return 'Person ' + this.getFullName();
}
}
class Employee extends Person {
constructor(firstName, lastName, jobTitle) {
super(firstName, lastName);
this.jobTitle = jobTitle;
}
toString() {
return `${super.toString()} (${this.jobTitle})`;
}
}
var john = new Person('John', 'Doe');
var wiktor = new Employee('Wiktor', 'Toporek', 'PHP Programmer');
46. console.log(john.toString()); /*Person: John Doe*/
console.log(wiktor.toString()); //Person: Wiktor Toporek (PHP Programmer)
/* examples/class/04-dynamic-inheritance.js */
const CarMixin = {
ride() {
console.log('Riding...');
}
};
const SailingMixin = {
sail() {
console.log('Sailing...')
}
};
function mixin(...mixins) {
var base = function() {};
Object.assign(base.prototype, ...mixins);
return base;
}
class Amphibian extends mixin(CarMixin, SailingMixin) {}
var amphibian = new Amphibian();
amphibian.ride(); /*Riding...*/
amphibian.sail(); /*Sailing...*/
47. /* examples/class/05-gettersnsetters.js */
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
this._scale = 1;
}
get area() {
return this.width * this.height;
}
set scale(newScale) {
var prevScale = this._scale;
this._scale = newScale;
this.width *= newScale / prevScale;
this.height *= newScale / prevScale;
}
get scale() {
return this._scale;
}
}
var rect = new Rectangle(10, 10);
console.log(rect.area); /*100*/
rect.scale = 0.5;
console.log(rect.area); /*25*/
48. rect.scale = 1;
console.log(rect.area); //100
/* examples/class/06-static.js */
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
static distance(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.sqrt(dx*dx + dy*dy);
}
}
const p1 = new Point(5, 5);
const p2 = new Point(10, 10);
console.log(Point.distance(p1, p2));
51. WADYWADY
Mamy kilka sposobów tworzenia obiektów. Po co kolejny?
Brakuje Access modifierów dla metod np. private
Brak wsparcia dla deklarowania właściwości klasy
53. /* examples/promise/01-promise.js */
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)]);
})
64. NA CO ZWRÓCIĆ UWAGĘ?NA CO ZWRÓCIĆ UWAGĘ?
Część ficzerów wymaga babel/polyfill
Proxy nie możliwe do przetłumaczenia na ES5