Skip to content

Instantly share code, notes, and snippets.

@Stoffo
Created December 7, 2015 16:43
Show Gist options
  • Save Stoffo/83b3f30f30817058a704 to your computer and use it in GitHub Desktop.
Save Stoffo/83b3f30f30817058a704 to your computer and use it in GitHub Desktop.
A few simple Examples in EcmaScript6 to play around with.
const PI = 3.1415926535;
function foo(a = 123, b = '') {
return PI
}
function bar(a, b, c, ...x) {
return x.length;
}
class Animal {
constructor(name, sex, weight = 0) {
this.name = name;
this.sex = sex === 'male' ? sex : 'female';
this.weight = weight;
}
move(distance){
this._distance += distance;
}
get distance() { return this.distance}
}
class Dog extends Animal {
constructor(name, sex, weight, race, toy, ...optionalData) {
super(name, sex, weight);
this.race = race;
this.toy = toy;
this.optional = optionalData;
}
static bark(msg = 'Whooooof!!!') {
return msg;
}
sprint(distance){
Dog.bark('Sprinting!!!');
this.move(distance);
}
}
var dog = new Dog('Bello', 'male', 23);
dog.sprint(50);
function promiseExample() {
return Promise((resolve, reject)
=> {setTimeout(() => resolve('msg'), 100)})
}
//Currencies:
var toEuro = Intl.NumberFormat('de-DE', {style : 'currency', currency: 'EUR'});
var toDollar = Intl.NumberFormat('en-US', {style : 'currency', currency: 'USD'});
toEuro.format(123456.457);
toDollar.format(123456.457);
function letExample() {
console.log(x);
var x = 123; //undefined
console.log(y);
let y = 456; //ReferenceError: y is not defined
}
//Dates:
var i10nEN = new Intl.DateTimeFormat("en-US");
var i10nDE = new Intl.DateTimeFormat("de-DE");
i10nEN.format(new Date("2015-01-02")) === "1/2/2015";
i10nDE.format(new Date("2015-01-02")) === "2.1.2015";
var sum = (x,y) => x + y;
var returnObject = id => ({id: id, msg: 'foo'});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment