Skip to content

Instantly share code, notes, and snippets.

@mariusschulz
Last active October 13, 2016 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariusschulz/2d9caa16ea85c0247f75411d67fcf095 to your computer and use it in GitHub Desktop.
Save mariusschulz/2d9caa16ea85c0247f75411d67fcf095 to your computer and use it in GitHub Desktop.
const square = function(x) {
return x * x;
};
const cube = (x) => {
return x * x * x;
};
const numbers = [11, 20, 33, 40, 55];
const isEven = (n) => n % 2 === 0;
const halve = (value) => value / 2;
const result = numbers
.filter(isEven)
.map(halve)
.sort();
console.log(result);
const names = [
{ name: "B" },
{ name: "A" }
].map(person => person.name).sort();
console.log(names);
const sayHi = () => console.log("hi");
const add = (a, b) => a + b;
function ViewModel() {
this.values = [];
ajax("https://some.api.com/endpoint.json", (values) => {
this.values = values;
});
}
// Block Scoping
function foo(a, b) {
if (a > b) {
let temp = a;
a = b;
b = temp;
}
console.log(temp);
}
foo(10, 5);
/*
var Circle = (function() {
function Circle(radius) {
this.radius = radius;
}
Circle.prototype.getArea = function() {
return Math.PI * Math.pow(this.radius, 2);
};
return Circle;
})();
*/
class Circle {
constructor(radius) {
this.radius = radius;
}
get area() {
return Math.PI * Math.pow(this.radius, 2);
}
static unit() {
return new Circle(1);
}
}
var circle = new Circle(10);
var area = circle.area;
console.log(area);
var unitCircle = Circle.unit();
console.log(unitCircle);
class Animal {
constructor(name) {
this.name;
}
}
class Cat extends Animal {
constructor(name) {
super(name);
// ...
}
}
class Dog extends Animal {
}
const cat = new Cat("Sally");
const numbers = [1, 2, 3];
numbers.push(4);
console.log(numbers);
const person = {
name: "Max",
age: 32
};
person.age++;
// const is ONE-TIME ASSIGNMENT
// not necessarily IMMUTABLE
function greet(greeting = "Hello") {
console.log(`${greeting} DWX 2016!`);
}
function greet(greeting) {
greeting = greeting || "Hello";
console.log(`${greeting} DWX 2016!`);
}
greet("Hi");
greet("");
function point(x, y) {
x = x || 100;
y = y || 100;
return { x, y };
}
console.log(point(0, 0));
const numbers = [1, 2, 3, 4, 5];
const [head, ...tail] = numbers;
console.log(head);
console.log(tail);
const point = { x: 10, y: 10, z: 10 };
// Destructuring of objects
function doSomething(point = {}) {
const { x = 1, y = 2 } = point;
console.log(x, y);
}
doSomething();
doSomething({});
doSomething({ x: 3 });
doSomething({ x: 3, y: 4 });
const x = 10;
const y = 20;
// Shorthand property syntax
const point = { x, y };
// Method syntax
const person = {
name: "Max",
// Previously:
// sayHi: function() {
sayHi() {
console.log(`Hi, I am ${this.name}.`);
}
};
// Computed property names
const person2 = {
["na" + "me"]: "Max"
};
const obj2 = {
[Symbol.iterator]() {
let count = 0;
let done = false;
return {
next() {
if (count > 5) {
done = true;
} else {
count++;
}
return { done, value: count };
}
};
}
};
for (let x of obj2) {
console.log(x)
}
let actions = [];
for (let i = 0; i < 5; i++) {
actions.push(function() {
console.log(i);
});
}
actions.forEach(function(action) {
action();
});
// IIFE
(function() {
}());
function nonNegative(...args) {
return args.filter(function(value) {
return value >= 0;
});
}
console.log(
nonNegative(1, -2, 3, -4, 5)
);
const listA = [1, 2, 3];
const listB = [4, 5];
// Array literals
const listC = [0, ...listA, ...listB, 6];
console.log(listC);
// Function calls
const values = [10, 30, 20, 40, 0];
const max = Math.max(...values);
console.log(max);
const symbol = Symbol();
console.log(typeof symbol);
console.log(symbol);
const obj = { bar: 43 };
obj[symbol] = "foo";
console.log(obj[symbol]);
console.log(JSON.stringify(obj));
console.log(Object.getOwnPropertySymbols(obj));
const firstName = "Peter";
const lastName = "Pan";
const fullName1 = firstName + " " + lastName;
const fullName2 = `${firstName} ${lastName}`;
console.log(fullName2);
// Multiline strings
const text = `line 1
line 2
line 3`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment