New JavaScript version
Marián Rusnák
PV226
Srping 2015
Developing web applications 4+ years
PHP, JavaScript, ASP.NET, Java
Master's student (2nd year)
Attending PV226 in previous years
Thesis under doc. Pitner
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
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
Well...that's not absolutely true
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 of AtScript?
Everybody agrees to collaborate
and align with ES7
Someday we might see types in JavaScript
Marián Rusnák