JavaScript, past 'til present


@giftkugel

Simon Skoczylas Senior Software Developer
Materna GmbH

Java, JavaScript
Web, Frontend

Agenda

  1. Motivation
  2. JavaScript usage
  3. ECMAScript 6
  4. Node.js

Motivation

JavaScript?

Facts

  • Since 1995
  • Mocha → LiveScript → JavaScript
  • On client- & server-side from the beginning
    • Netscape Navigator
    • Netscape Enterprise Server
  • Since 1997 part of Ecma International → ECMAScript
    • ECMAScript 1 (1997)
    • ECMAScript 2 (1998)
    • ECMAScript 3 (1999)
    • ECMAScript 4 (the incomplete)
    • ECMAScript 5 (2009)
    • ECMAScript 6 (2015)
    • ECMAScript 7 (work in progress)

JavaScript!!!

  • JavaScript vs ECMAScript
    • Web browser vs standard
    • JavaScript ~ ECMAScript 3/5/(6) + DOM API
    • window, document, ...
  • JavaScript Versions (John Resig, 2008)
  • Nashorn (Java), Adobe ExtendedScript (Photoshop etc.), JScript .NET, ...

JavaScript Versions (John Resig, 2008)

  • IE 6-7 support JScript 5 (which is equivalent to ECMAScript 3, JavaScript 1.5)
  • IE 8 supports JScript 6 (which is equivalent to ECMAScript 3, JavaScript 1.5 – more bug fixes over JScript 5)
  • Firefox 1.0 supports JavaScript 1.5 (ECMAScript 3 equivalent)
  • Firefox 1.5 supports JavaScript 1.6 (1.5 + Array Extras + E4X + misc.)
  • Firefox 2.0 supports JavaScript 1.7 (1.6 + Generator + Iterators + let + misc.)
  • Firefox 3.0 supports JavaScript 1.8 (1.7 + Generator Expressions + Expression Closures + misc.)
  • The next version of Firefox will support JavaScript 1.9 (1.8 + To be determined)
  • Opera supports a language that is equivalent to ECMAScript 3 + Getters and Setters + misc.
  • Safari supports a language that is equivalent to ECMAScript 3 + Getters and Setters + misc.

ECMAScript

  • As JavaScript one of the most important web technologies
    • Beside HTML and CSS
  • Interpreted at run-time
  • Prototype-based
  • Supports different programming paradigms
    • object-orientated (prototyps)
    • imperativ / procedural
    • functional
  • Not typed at development time, but at run-time
    • But: TypeScript (Microsoft, 2012)

Language Trends on GitHub (08.2015)

TIOBE Index (09.2015)

TIOBE Hall of Fame

The Dawn Of Enterprise JavaScript

JavaScript Finds Its Place In Enterprise Software Development

Adoption of JavaScript — and the Node.js runtime environment in particular — sets the stage for the biggest shift in enterprise application development in more than a decade

ECMAScript!!!

Usage of JavaScript

Maybe not for Mission Control

  • Hardware, device drivers, etc.

For the web & desktop

Frontend

  • Web browser
    • Generally known
  • At the beginning in use for "dynamic" content with out server connection
  • AJAX brought the possibility to access data from server (2005)
  • Easy to use because of JQuery (2006)

Single-Page-Apps

  • Business-Logic inside the web browser
    • First request loads the whole logic into the client
    • Content reloading possible
  • AngularJS by Google (2009)
    • Often in use as MEAN-Stack
    • MongoDB
    • ExpressJS
    • AgularJS
    • NodeJS

Backend

Synergy effects

Making Netflix.com Faster

  • Before
    • Front-end vs back-end
    • JavaScript with jQuery at the front-end
    • Java with Tomcat, Struts and Tiles at the back-end
  • After
    • JavaScript at front-end and back-end (with NodeJS and ReactJS)
    • Possibility to load balance between front-end and back-end
    • No context switch between languages
    • Identical API to generate HTML for the Front-end and back-end

Isomorphic JavaScript

What's new in ECMAScript 6

Variable scope

  • Currently (var) global object (e.g. window), or function
  • Now: let

		function doSomethingECMA5()  {
			var a = ['apple', 'banana'];
			for (var i = 0; i < a.length; i++) {
				console.log(i, a[i]);
			}
			console.log(i, a[i]);  // undefined, weil [0, 1]
		}
	

		function doSomethingECMA6()  {
			var a = ['apple', 'banana'];
			for (let i = 0; i < a.length; i++) {
				console.log(i, a[i]);
			}
			console.log(i, a[i]); // Nicht möglich
		}
	

Constants

  • Currenly only by convention → UPPER CASE
  • Or with object properties

		const MAX_COUNT = 5;

		console.log(MAX_COUNT); // 5

		MAX_COUNT = 6; // use strict = Error

		console.log(MAX_COUNT);
	

String

  • New functions
  • startsWith, endsWith, includes

		console.log('hello'.startsWith('hell')); // true

		console.log('hello'.endsWith('hell')); // false

		console.log('hello'.includes('ell')); // true
	

Arrow functions


		var quadrat = x => x*x;

		var result = quadrat(5);

		console.log(result); // 25
	

		var addition = (x,y) => x+y;

		var result = addition(5, 6);

		console.log(result); // 11
	

		() => { };
		x => { };
		(x, y) => { };
	

Spread operator

  • Not well supported at the moment

		function apple(a, b) {
		    for (var i = 0; i < arguments.length; i++) {
		        console.log(arguments[i]);
		    }
		}


		apple('eins', 'zwei', 'drei');


		function banana(a, b, ...c) {
		    c.forEach(function(element) {
		        console.log(element);
		    });
		}

		banana('eins', 'zwei', 'drei');
	

Default parameter values

  • Not well supported at the moment

		function tomato(a, b, c = 'three') {
		    console.log(a, b, c);
		}

		tomato('one', 'two'); // one, two, three
	

Iterators

  • Not well spoorted at the moment

		var bands = ['Metallica', 'Queen', 'The Police', 'The Beatles'];

		var iterator = bands.values();
		iterator.next();
	

Generators


		function* counter() {
		    yield 1;
		    yield 2;
		}

		var someCounter = counter();
		var result = someCounter.next();

		console.log(result); // { value: 1, done: false }

		result = someCounter.next();

		console.log(result); // { value: 2, done: false }

		result = someCounter.next();

		console.log(result); // { value: undefined, done: true }
	

Modules


		export function say() {
			   return 'hello world';
		}
	

		import {say} from 'export';

		console.log(say());
	

Classes


		class Person {
		    constructor(firstname, lastname) {
		        this.firstname = firstname;
		        this.lastname = lastname;
		    }

		    toString() {
		        return this.firstname + ' ' + this.lastname;
		    }
		}

		var max = new Person('Max', 'Schmidt');

		console.log(max.toString()); // Max Schmidt
	

Classes


		class Employee extends Person {
		    constructor(firstname, lastname, id) {
		        super(fristname, lastname);
		        this.id = id;
		    }

		    toString() {
		        return this.id + ': ' + this.firstname + ' ' + this.lastname;
		    }
		}

		var anton = new Employee('Anton', 'Mayer', 1);

		console.log(anton.toString()); // 1: Anton Mayer
	

NodeJS

Facts

  • JavaScript for the server
  • V8 by Google, written in C
  • Non blocking I/O
    • JavaScript Event-Loop
  • Event-driven
  • Single-Thread (actually)
  • nodejs.org
  • Linux, MacOS, Windows, SunOS, ARM
    • 32bit & 64bit
    • ARM6, ARM7, ARM8
  • As of late 4.0.0, before 0.x.y
  • Long Term Support (LTS)

NPM

  • Node Package Manager
  • ~183.000 Module at npmjs.com
  • package.json describes the project
  • Able to load libraries on demand
  • Private NPM!

Documentation and API

Default modules

  • Assert (similar to JUnit)
  • Addons (C/C++)
  • Buffer
  • Child Process
  • Cluster
  • Console
  • Crypto
  • Debugger (Remote)
  • DNS
  • Errors
  • Events
  • File System
  • HTTP / HTTPS
  • Modules
  • net
  • OS
  • Path
  • process
  • punycode (Encode, Decode)
  • Query String
  • Readline
  • Stream
  • StringDecoder
  • Timers (setTimeout, etc.)
  • TLS (SSL)
  • UDP / Datagram Sockets
  • URL
  • util
  • VM
  • Zlib

ExpressJS

"Fast, unopinionated, minimalist web framework for Node.js"

  • Most kown web framework for NodeJS
  • Acts as middleware
  • Templates (Jade, Handlebars, Hogan)
  • CSS Preprocessor (LESS, Stylus, Compass, SASS)
  • expressjs.com

Questions?

// http://kangax.github.io/compat-table/es6/ // Verwendbar über Featuredetection und Polyfills