Ecmascript Oder ?
Total Page:16
File Type:pdf, Size:1020Kb
ECMAScript oder ? - das ist hier die Frage - Orientation in Objects GmbH Weinheimer Str. 68 68309 Mannheim www.oio.de Version: 1.1 [email protected] Ihr Sprecher Thorsten Maier Trainer, Berater, Entwickler Schwerpunkte Architektur Prozesse @ThorstenMaier Qualitätssicherung © Orientation in Objects GmbH ECMAScript vs. TypeScript 2 JavaScript hat(te) einen schlechten Ruf © Orientation in Objects GmbH ECMAScript vs. TypeScript 3 “The specification is of “JavaScript has more in extremely poor quality.” common with functional languages like Lisp or Scheme than with C or Java” The World's Most Misunderstood Programming Language Douglas Crockford “Most of the people writing in 2001 JavaScript are not programmers.” “Nearly all of the books about “JavaScript has its share JavaScript are quite awful.” of design errors.” © Orientation in Objects GmbH ECMAScript vs. TypeScript 4 Alles besser mit ECMAScript? Ein wenig Historie © Orientation in Objects GmbH ECMAScript vs. TypeScript 5 Juni 1997 ES 1.0 Dez. 2009 Juni 2015 ES 5 ES 2015 JavaScript JScript ActionScript 1999 ES 4 Yahoo, Google, … JS wird professionell 1995 März 1996 Brendan Eich Netscape 2.0 / JS 1.0 Mocha / LiveScript IE 3.0 / JScript 10 Tage © Orientation in Objects GmbH ECMAScript vs. TypeScript 6 ES 5 ES 2015 2009 2015 JavaScript wird professionell © Orientation in Objects GmbH ECMAScript vs. TypeScript 7 for (var i = 0; i < 5; i++) { for (var i = 0; i < 5; i++) { var text = "Hello " + i; let text = "Hello " + i; } } console.log(text); console.log(text); Block-Scoped Variablen © Orientation in Objects GmbH ECMAScript vs. TypeScript 8 var Company = (function () { function Company(name) { class Company { this.name = name; constructor(name) { } this.name = name; Company.prototype.getName = function () { } return this.name; getName() { }; return this.name; return Company; } }()); } Klassen © Orientation in Objects GmbH ECMAScript vs. TypeScript 9 class Car { drive() { console.log("Car is driving."); } } - class Cabrio extends Car { openTop() { console.log("Top is open."); } } Vererbung © Orientation in Objects GmbH ECMAScript vs. TypeScript 10 var Person = (function () { function Person() { class Person { } sayYourSpecies() { Person.sayYourSpecies = function () { static console.log("Ich bin ein Mensch"); console.log("Ich bin ein Mensch"); }; } return Person; } }()); Person.sayYourSpecies(); Person.sayYourSpecies(); Statische Elemente © Orientation in Objects GmbH ECMAScript vs. TypeScript 11 constants.js export const HELLO_WORLD = "Hello World"; - index.js import { HELLO_WORLD } from ‘./constants.js' Module © Orientation in Objects GmbH ECMAScript vs. TypeScript 12 var add = function (a, b) { return a + b; var add = (a, b) => a + b; }; Arrow Functions © Orientation in Objects GmbH ECMAScript vs. TypeScript 13 var name = "Thorsten"; let name = "Thorsten"; var t = "Hallo " + name + "!"; let t = `Hallo ${name}!`; Template Strings © Orientation in Objects GmbH ECMAScript vs. TypeScript 14 ES 2015 - Browser Support © Orientation in Objects GmbH ECMAScript vs. TypeScript 15 var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; class Foo { }; }(); bar(x) { function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } return `foo bar ${x}`; } } var Foo = function () { function Foo() { _classCallCheck(this, Foo); } } const foo = new Foo(); _createClass(Foo, [{ key: "bar", console.log(foo.bar("baz")); value: function bar(x) { return "foo bar " + x; } }]); return Foo; ES 2015 }(); var foo = new Foo(); console.log(foo.bar("baz")); ES 5 © Orientation in Objects GmbH ECMAScript vs. TypeScript 16 Hört sich doch alles nicht schlecht an. Warum jetzt noch ? © Orientation in Objects GmbH ECMAScript vs. TypeScript 17 (TypeScript, Flow, …) Module (Babel, …) Type Plain scripts systems Compilers Systems © Orientation in Objects GmbH ECMAScript vs. TypeScript 18 © Orientation in Objects GmbH ECMAScript vs. TypeScript 19 ECMAScript that scales © Orientation in Objects GmbH ECMAScript vs. TypeScript 20 Statisch typisiertes Superset von ECMAScript Kompiliert zu ECMAScript © Orientation in Objects GmbH ECMAScript vs. TypeScript 21 2.5 ES 2015 ES 2016 ES 2017 ES 5 Block-Scope Collections ** Operator await/async 2.0 Funktionale Module Arrow Array.prototype.includes Shared memory Statisches Typsystem Programmierung Klassen functions atomics Properties Generatoren … … Promises © Orientation in Objects GmbH ECMAScript vs. TypeScript 22 Open Source © Orientation in Objects GmbH ECMAScript vs. TypeScript 23 Gute Tool-Unterstützung Features from future today © Orientation in Objects GmbH ECMAScript vs. TypeScript 24 CODE COMPLETION ECMAScript © Orientation in Objects GmbH ECMAScript vs. TypeScript 25 CODE COMPLETION TypeScript © Orientation in Objects GmbH ECMAScript vs. TypeScript 26 CODE SMARTER © Orientation in Objects GmbH ECMAScript vs. TypeScript 27 CODE SMARTER lib.es6.d.ts JS Dev Kopfwissen?! © Orientation in Objects GmbH ECMAScript vs. TypeScript 28 CODE SMARTER Finde den Fehler © Orientation in Objects GmbH ECMAScript vs. TypeScript 29 CODE SMARTER © Orientation in Objects GmbH ECMAScript vs. TypeScript 30 CODE CONFIDENTLY © Orientation in Objects GmbH ECMAScript vs. TypeScript 31 CODE BETTER (TODAY) class Person { var Person = /** @class */ (function () { name: string; function Person(name) { this.name = name; constructor(name: string) { } this.name = name; Person.prototype.sayHello = function () { } console.log("Hello my name is " + this.name + "!"); sayHello() { }; console.log(`Hello my name is return Person; ${this.name}!`); }()); } } app.ts app.js (ECMAScript 5) © Orientation in Objects GmbH ECMAScript vs. TypeScript 32 CODE BETTER (TODAY) function sortPersonByName(a: Person[]) function sortPersonByName(a) { { var result = a.slice(0); var result = a.slice(0); result.sort(function (x, y) { result.sort((x, y) => return x.name.localeCompare(y.name); x.name.localeCompare(y.name)); }); return result; return result; } } app.ts app.js (ECMAScript 5) © Orientation in Objects GmbH ECMAScript vs. TypeScript 33 CODE BETTER (TODAY) var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { async function fetchJson(url) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); try { }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; let request = await fetch(url); return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); let text = await request.text(); while (_) try { if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [0, t.value]; switch (op[0]) { return JSON.parse(text); case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; } catch (e) { default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } console.log(`ERROR: ${e.stack}`); if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } }; function fetchJson(url) { return __awaiter(this, void 0, void 0, function () { var request, text, error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); fetchJson( return [4 /*yield*/, fetch(url)]; case 1: request = _a.sent(); return [4 /*yield*/, request.text()]; 'http://example.com/some_file.json') case 2: text = _a.sent(); return [2 /*return*/, JSON.parse(text)]; case 3: .then(obj => console.log(obj)); error_1 = _a.sent(); console.log("ERROR: " + error_1.stack); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); app.ts } fetchJson('http://example.com/some_file.json') .then(function (obj) { return console.log(obj); }); ECMAScript 5 © Orientation in Objects GmbH ECMAScript vs. TypeScript 34 CODE BIGGER <3