Created
February 23, 2016 12:26
-
-
Save otobrglez/d5c893493c22208b2009 to your computer and use it in GitHub Desktop.
Splash of ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
// Experimenting with ES6 on NodeJS | |
// node --harmony --harmony_default_parameters ./little_es6.js | |
var numbers = [1, 1, 2, 5, 3]; | |
var littleSum = numbers.filter(x => x % 2).reduce((s, b) => s + b); | |
console.log(`Sum is ${littleSum}.`); | |
numbers.forEach(l => console.log(l)); | |
for (let n of numbers.filter(x => x % 2 == 0)) console.log(n); | |
console.log(numbers.map((x) => { | |
return x * x | |
}).join(", ")); | |
var Something = class { | |
constructor(pSex = 'm') { | |
this.sex = pSex; | |
} | |
}; | |
var Person = class extends Something { | |
constructor(first_name, last_name, sex) { | |
super(sex); | |
this.first_name = first_name; | |
this.last_name = last_name; | |
return this; | |
} | |
get firstName() { | |
return this.first_name; | |
} | |
get lastName() { | |
return this.last_name; | |
} | |
get fullName() { | |
return `${this.firstName} ${this.lastName}` | |
} | |
toString() { | |
return this.fullName + ", " + this.sex; | |
} | |
}; | |
let oto = new Person("Oto", "Brglez"); | |
console.log(oto.toString()); | |
console.log(oto.firstName); | |
let people = [new Person("Martina", "H.", "f"), new Person("Oto", "B."), new Person("Teja", "B.", "f")]; | |
console.log(people.filter(p => p.sex == "f").sort((a, b)=> { | |
if (a.last_name > b.last_name) return 1; | |
if (a.last_name < b.last_name) return -1; | |
return 0; | |
})); | |
const Source = class extends require('events').EventEmitter {}; | |
var s = new Source; | |
s.on('init', (event) => { | |
console.log(`Got ${event}`); | |
s.emit('hello', "Oto"); | |
}); | |
s.on('hello', (event)=> { | |
console.log(`Hello ${event}!`); | |
}); | |
s.emit("init", "oto"); | |
const sum = (a = 0, b = 0) => a + b; | |
console.log(sum(10) == 10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment