ECMA Script 6

Shota Papiashvili

08/04/2015

Walla!News

Agenda

  • ECMA?
  • Variables and Scopes
  • Arrays
  • Functions
  • OOP
  • Export / Import
  • Templates
  • Loops
  • Generators
  • Syntactic sugar
  • When?

ECMA?

Ecma International is an international, private (membership-based) non-profit standards organization for information and communication systems.

 

They create the standarts to verious of thinks like CD-Rom, C#, Javascript, 3D File format and more...

Variables and Scopes

function swap(x, y){
   if (x !== y){
      var temp = x;
      x = y;
      y = temp;
   }
   
   console.log( temp );
   return [x, y];
};
function swap(x, y){
   if (x !== y){
      let temp = x;
      x = y;
      y = temp;
   }
   
   console.log( temp ); // ERROR
   return [x, y];
};

let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. This is unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope

Variables and Scopes

var funcs = [];

for (var l = 3; l; l--) {
   funcs[l] = function() {
      return l;
   }
}

for (var i = 1; i < funcs.length; i++)
   console.log(funcs[i]());
var funcs = [];

for (let l = 3; l; l--) {
   funcs[l] = function() {
      return l;
   }
}

for (var i = 1; i < funcs.length; i++)
   console.log(funcs[i]());   
// OUTPUT:
// 0
// 0
// 0
// OUTPUT:
// 1
// 2
// 3

Variables and Scopes

const PI = 3.14159265359;
let name = { first: 'Jane', last: 'Doe' };
let { first: f, last: l } = name; // var f = name.first, l = name.last;
let name = { first: 'Jane', last: 'Doe' };
let { first, last } = name;  // var first = name.first, last = name.last;
function getDates(){
   var d = new Date();
   return { today: d.getDate(), tommorow: d.getDate() +1 };
}

let {today, tommorow} = getDates();

Arrays 

let [x, y] = [ 'a', 'b' ]; // x='a', y='b'
let [x, y, ...rest] = [ 'a', 'b', 'c', 'd' ]; 
                    // x='a', y='b', rest = [ 'c', 'd' ]
[x,y] = [y,x];

/*  var temp = x;
    x = y;
    y = x;        */

Multiple declarations

Swaping

Functions

function adder(x, y = 5){ 
    return x+y;
}
function adder(x, y){
    y = y || 5; 
    return x+y;
}
function concat(separator) {
  var args = 
    Array.prototype.slice.call(arguments, 1);
  return args.join(separator);
}
function concat(separator, ...others) {
  return others.join(separator);
}

Arrow Expressions

let squares = [1, 2, 3].map(x => x * x);
var squares = [1, 2, 3].map(function (x) {
    return x * x
});

Rest of the parameters

Default values

OOP

function human(name, age){
    this.name    = name;
    this.age     = age;
}

human.prototype.sayMyName = function(){
    console.log(this.name);
}

human.prototype.sayMyAge = function(){
    console.log(this.name);
}

var s = new human('shota', 25);
class human {
    constructor(name, age){
        this.name    = name;
        this.age     = age;
    }

    sayMyName() {
        console.log(this.name);
    }

    sayMyAge() {
        console.log(this.age);
    }
}

var s = new human('shota', 25);

We now can define classes like in any other OOP language

OOP

class human {
    constructor(name, age){
        this.name    = name;
        this.age     = age;
    }

    sayMyName() {
        console.log(this.name);
    }

    sayMyAge() {
        console.log(this.age);
    }
}

var s = new human('shota', 25);
class prograqmmer extends human {
    constructor(name, age, lang) {
        super(x, y);
        this.lang = lang;
    }
    sayMyLang() {
        console.log(this.lang);
    }
}

We now can extend the class and override methods.

we also can call to the super to use the father's function

Export / Import

// func.js
export function foo(x){
    return x * x;
}
export const PI = 3.1415;
let z = 3; // not exported
// main1.js
import {foo} from 'func';
console.log(foo(2));
console.log(PI); // ERROR
// main2.js
import * from 'func';
console.log(foo(2));
console.log(PI);
// func.js
export default function (...) { ... };

// main3.js
import func from 'func';

With export and import we can inject our javascript code (like requireJS)

Templates

let name = 'Shota';
let helloStr = `My name is ${name}`;

ES6 uses ` to define template strings, ' and " still define normal strings

let name = 'Shota';
let helloStr = `My name is ${name}
i also can be multi
line text`;

Loops

let arr = ['hello', 'world'];

for (let elem of arr) {
    console.log(elem);
}
let arr = ['hello', 'world'];

for (let [i, elem] of arr.entries()) {
    console.log(i, elem);
}

Works for iterables

for-of

Generators

function* func() {
    yield 0;
    yield 1;
}

let obj = func();
console.log(obj .next()); // { value: 0, done: false }
console.log(obj .next()); // { value: 1, done: false }
console.log(obj .next()); // ( value: undefined, done: true }
function* iterEntries(obj) {
    let keys = Object.keys(obj);
    for (let i=0; i < keys.length; i++) {
        let key = keys[i];
        yield [key, obj[key]];
    }
}

let myObj = { foo: 3, bar: 7 };
for (let [key, value] of iterEntries(myObj)) {
    console.log(key, value);
}

Implementing an iterator

Generators

function* gen(){
  var res = Math.pow(yield, yield);
  return res;
}

let g = gen();
console.log(g.next().value);
console.log(g.next(10).value);
console.log(g.next(2).value);
function* gen(){
  var res = Math.pow(yield, yield);
  return res;
}

let g = gen();
console.log(g.next().value);
console.log(g.next(10).value);
console.log(g.next(2).value);


// will print 100, WHY?

Maps and Sets

let map = new Map();
let obj = {};

map.set(obj, 123);
console.log(map.get(obj)); // 123
console.log(map.has(obj)); // true
map.delete(obj);
console.log(map.has(obj)); // false

Maps

Data structure mapping from arbitrary values to arbitrary values 

let set1 = new Set();

set1.add('hello');
console.log(set1.has('hello')); // true
console.log(set1.has('world')); // false
let set2 = new Set([3,2,1,3,2,3]);
console.log(set2.values()); // 1,2,3

Sets

A collection of values without duplicates. 

Syntactic sugar

'abc'.repeat(3);            // 'abcabcabc'
'abc'.startsWith('ab');     // true
'abc'.endsWith('bc');       // true
'foobar'.contains('oo');    // true

Strings

[13, 7, 8].find(x => x % 2 === 0)       // 8
[1, 3, 5].find(x => x % 2 === 0)        // undefined
[13, 7, 8].findIndex(x => x % 2 === 0)  // 2
[1, 3, 5].findIndex(x => x % 2 === 0)   // -1

Arrays

class Point {
    constructor(x, y) {
        Object.assign(this, { x, y });
    }
}

Object.assign Merge one object into another one.

When?

  • End of 2014: specification is finished.
  • March 2015: publication process starts.
  • June 2015: formal publication.

For the meanwhile: babeljs.io

Its published!

 

(http://www.ecma-international.org/ecma-262/6.0/ECMA-262.pdf)(566 pages PDF) 

but we dont know when browsers will support it.

ECMAScript 6

By Shota Papiashvili

ECMAScript 6

intro to ECMAScript 6

  • 1,322