New JavaScript version
Marián Rusnák
WebElement Prešov
May 2015
Developing web applications 4+ years
PHP, JavaScript, ASP.NET, Java
Master's student (final year)
Masaryk University, Brno
Ehm...
// Constructor
function Hello(name) {
this.name = name;
}
// Object public method
Hello.prototype.hello = function hello() {
return 'Hello ' + this.name + '!';
};
// Static method
Hello.sayHelloAll = function () {
return 'Hello everyone!';
};
Nice!
Well, not really
function foo() {
var x = 5;
console.log(x); // 5
if (true) {
// in a block
var x = 10; // overwrites
}
console.log(x); // 10
console.log(y); // undefined
var y = 100;
}
function foo() {
var x = 5;
var y; // hoisted to the top
console.log(x); // 5
if (true) {
// in a block
var x = 10; // overwrites
}
console.log(x); // 10
console.log(y); // undefined
var y = 100;
}
var obj = {
val: 5,
superMethod: function () {
return this.val;
}
};
console.log(obj.superMethod()); // 5
function logMethod(method) {
this.val = 10;
console.log(method()); // this points to logMethod object
}
logMethod(obj.superMethod); // 10
define('myModule', [
'coolModule',
'fancyModule'
], function(dep1, dep2) {
// do some cool stuff
var privateVar = "I'm private.";
return {
publicVar: "I'm public.",
publicFunc: function () {
console.log('Hello world!');
}
}
});
var dep1 = require('coolModule');
var dep2 = require('fancyModule');
// do some cool stuff
var privateVar = "I'm private.";
module.export = {
publicVar: "I'm public.",
publicFunc: function () {
console.log('Hello world!');
}
};
class Shape {
constructor(x, y) {
this.x = x;
this.y = y;
}
logInfo() {
console.log('X: ' + this.x + ', Y: ' + this.y);
}
}
class Shape {
constructor(x, y) {
this.x = x;
this.y = y;
}
logInfo() {
console.log('X: ' + this.x + ', Y: ' + this.y);
}
}
class Circle extends Shape {
constructor(x, y, radius) {
super(x, y); // parent constructor
this.radius = radius;
}
// override method
logInfo() {
console.log('X: ' + this.x + ', Y: ' + this.y + ', Radius: ' + this.radius);
}
}
class Shape {
constructor(x, y) {
this.x = x;
this.y = y;
}
logInfo() {
console.log('X: ' + this.x + ', Y: ' + this.y);
}
}
class Circle extends Shape {
constructor(x, y, radius) {
super(x, y);
this.radius = radius;
}
logInfo() {
console.log('X: ' + this.x + ', Y: ' + this.y + ', Radius: ' + this.radius);
}
static hello() {
console.log("Hello I'm a circle with a static method.");
}
}
var odds = evens.map(v => v + 1);
var nums = evens.map((v, i) => v + i);
var pairs = evens.map(v => ({even: v, odd: v + 1}));
document.addEventListener('click', (event) => {
console.log(event.target);
});
Wow, I know it from C# and Java 8
var PageHandler = {
id: "123456",
init: function() {
document.addEventListener("click", (event) => {
this.doSomething(event.type); // this points to PageHandler
});
},
doSomething: function(type) {
console.log("Handling " + type + " for " + this.id);
}
};
Removes confusion around value of this
var name = "Bob"
var time = "today";
console.log(`Hello ${name}, how are you ${time}?`);
// Hello Bob, how are you today?
var count = 10;
var price = 0.25;
var message = `${count} items cost $${(count * price).toFixed(2)}.`;
console.log(message); // 10 items cost $2.50.
With calculations
var multiline = `Multiline
string`;
console.log(multiline); // Multiline
// string
var multiline = `Multiline string
with indentation`;
console.log(multiline); // Multiline string
// with indentation
function getValue(condition) {
if (condition) {
let value = "blue";
return value;
} else {
// value doesn't exist here
return null;
}
// value doesn't exist here
}
if (condition) {
console.log(value); // ReferenceError
let value = "blue";
}
var colors = [ "red", [ "green", "lightgreen" ], "blue" ];
// later
var [ firstColor, [ secondColor ] ] = colors;
console.log(firstColor); // "red"
console.log(secondColor); // "green"
var options = {
repeat: true,
save: false
};
// later
var { repeat: localRepeat, save: localSave } = options;
console.log(localRepeat); // true
console.log(localSave); // false
var options = {
repeat: true,
save: false
};
// later
var { repeat, save } = options;
console.log(repeat); // true
console.log(save); // false
function f(x, y = 12) {
// y is 12 if not passed (or passed as undefined)
return x + y;
}
console.log(f(3)); // 15
function f(x, ...y) {
// y is an Array
return x * y.length;
}
console.log(f(3, 'hello', true)); // 6
function f(x, y, z) {
return x + y + z;
}
// Pass each elem of array as argument
console.log(f(...[1,2,3])) // 6
function division({ num, divisor }) {
return num / divisor;
}
console.log(division({ num: 5 }); // Error
console.log(division({ num: 25, divisor: 5 }); // 5
import myModule from 'lib/myModule';
function doSomething() {
myModule.someMethod();
}
export doSomething;
// lib/logger.js
import textUtils from 'lib/textUtils';
export function log(message) {
...
textUtils.format(message);
...
}
export function warn(message) {
...
textUtils.format(message);
...
}
// myModule.js
import { log, warn } from 'lib/logger';
var myModule = {
doSomething: function () {
// do cool stuff
if (somethingWentWrong) {
warn('Ooops!');
} else {
log("I'm here!");
}
}
}
export myModule;
// myModule.js
import { log as myLog, warn } from 'lib/logger';
var myModule = {
doSomething: function () {
// do cool stuff
if (somethingWentWrong) {
warn('Ooops!');
} else {
myLog("I'm here!");
}
}
}
export myModule;
// myModule.js
import { log, warn } from 'lib/logger';
var myModule = {
doSomething: function () {
// do cool stuff
if (somethingWentWrong) {
warn('Ooops!');
} else {
log("I'm here!");
}
}
}
export default myModule;
function createIterator(items) {
var i = 0;
return {
next: function () {
var done = (i >= items.length);
return {
done: done,
value: !done ? items[i++] : undefined
};
}
};
}
var iterator = createIterator([1, 2, 3]);
do {
var progress = iterator.next();
console.log(progress.value);
} while (!progress.done);
function *createIterator(items) {
for (let i=0; i < items.length; i++) {
yield items[i];
}
}
let iterator = createIterator([1, 2, 3]);
console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: 3, done: false }
console.log(iterator.next()); // { value: undefined, done: true }
Well...that's not absolutely true
...and server
Compile ES6 to ES5
Fragmentation again?
TypeScript - language of Angular 2
http://blogs.msdn.com/b/typescript/archive/2015/03/05/angular-2-0-built-on-typescript.aspx
Dark future for AtScript
Everybody agrees to collaborate
and align with ES7
Someday we might see static types in JavaScript
Marián Rusnák