Skip to content

Instantly share code, notes, and snippets.

View wklug's full-sized avatar

Wolfgang Klug wklug

View GitHub Profile
@wklug
wklug / js-gist-07.js
Created October 27, 2016 04:01
JS + ES6 - Built-in types
/**
* JS + ES6 - Buit-in types.
*
* There are 6 built-in types in JavaScript (undefined, string, number, boolean, null, object). Plus, a new one
* introduced with ES6 (symbol).
**/
var foo;
console.log(typeof foo); // "undefined"
var bar = 'P. Nutt';
@wklug
wklug / js-gist-06.js
Created October 27, 2016 03:46
ES6 - Destructuring assignment
/**
* ES6 - Destructuring assignment
**/
var [person1, person2, person3, ...otherPeople] = [
{
name: 'P. Nutt',
age: 18
},
{
name: 'Hazel Nutt',
@wklug
wklug / js-gist-05.js
Created October 27, 2016 03:45
ES6 - Default function parameters
/**
* ES6 - Default function parameters.
*
* Define default value for empty function parameters.
**/
function sayHello(name, isPirate = false) {
if (isPirate) {
console.log(name + ' says: Arrr! This be good grog!'); // Manny Bones says: Arrr! This be good grog!
} else {
console.log(name + ' says: Hi, how are you?'); // P. Nutt says: Hi, how are you?
@wklug
wklug / js-gist-04.js
Created October 27, 2016 03:44
ES6 - Empty Arrow functions return
/**
* ES6 - Arrow functions.
*
* Empty arrow functions return `undefined`. The same as normal functions.
**/
let emptyness = () => {};
console.log(emptyness()); // undefined
var completelyEmpty = function() {
};
@wklug
wklug / js-gist-03.js
Last active October 27, 2016 03:42
ES6 - Arrow functions working with "this"
/**
* ES6 - Arrow functions.
*
* Arrow functions don't rebind `this`. Comparison between old `function` approach and `arrow function` approach.
**/
function Person0(name, age) {
this.name = name;
this.age = age;
setTimeout(function() {
@wklug
wklug / js-gist-02.js
Created October 25, 2016 15:21
JavaScript curiosities
/**
* Difference between implicit and explicit global variables
**/
var myGlobal1 = 'P. Nutt';
myGlobal2 = 'Hazel Nutt';
delete myGlobal1; // false
delete myGlobal2; // true
console.log(myGlobal1); // P. Nutt
console.log(myGlobal2); // ReferenceError
@wklug
wklug / js-gist-01.js
Created October 14, 2016 21:12
ES6 - Arrow functions are always anonymous.
/**
* ES6 - Arrow functions.
*
* Arrow functions are always anonymous. Useful for one line callbacks.
**/
const people = [
{
name: 'P. Nutt',
age: 18
},
@wklug
wklug / js-gist-00.js
Created October 14, 2016 21:09
ES6 - var, let, const. The temporal dead zone.
/**
* ES6 - var, let, const. The temporal dead zone.
*
* According to ECMAScript 6 specification. https://tc39.github.io/ecma262/#sec-let-and-const-declarations
* let and const declarations define variables that are scoped to the running execution context's
* LexicalEnvironment. The variables are created when their containing Lexical Environment is instantiated but may
* not be accessed in any way until the variable's LexicalBinding is evaluated. A variable defined by a
* LexicalBinding with an Initializer is assigned the value of its Initializer's AssignmentExpression when the
* LexicalBinding is evaluated, not when the variable is created. If a LexicalBinding in a let declaration does
* not have an Initializer the variable is assigned the value undefined when the LexicalBinding is evaluated.