Skip to content

Instantly share code, notes, and snippets.

@mariusGundersen
mariusGundersen / index.md
Last active August 29, 2015 14:03
Promise.factory

The ES6 Promise specification contains the very useful constructor for creating a promise, like so:

var promise = new Promise(function(resolve, reject){
  resolve("hi");
});

promise.then(function(message){
  console.log(message);
});
@mariusGundersen
mariusGundersen / labelledBlocks.md
Last active August 29, 2015 14:02
labelled blocks

JavaScript has labels, which can be placed before blocks to create a named section of code, serving almost the same use as an imidiately invoked function expression (IIFE)

(function iife(){
  var scoped = "this is scoped stuff";
})();

iife: {
  let scoped = "this is scoped";
}

Currently you can export things from a module in at least six different ways, and import things in at least six different ways, resulting in 36 different combinations, most of which are not semantically valid.

Here is a greatly simplified (and probably naive) suggestion for modules in ES6:

###export You can only export named things, including variables and functions.

let a = "hello";
export a;
@mariusGundersen
mariusGundersen / defaultExport.js
Created June 9, 2014 20:41
Peculiarities in ES6 modules
//The file foobar.js contains these two lines of code
var foo = "foo", bar = "bar";
export default = {foo, bar};
//It can now be imported into another module using either of these two lines:
import foobar from "foobar";
module foobar from "foobar";
//But not this line:
import {foo, bar} from "foobar";
@mariusGundersen
mariusGundersen / moduleImports.js
Created June 9, 2014 20:05
Potential module import syntax in ES6
module _ from "underscore";
import _ from "underscore";
import module _ from "underscore";
import {each, map, find} from "underscore";
import "underscore";
import default _ from "underscore";
const _ = System.import("underscore");
import "underscore" as _;
import {_} from "underscore";
import {each as forEach} from "underscore";
@mariusGundersen
mariusGundersen / weakEventListeners.js
Last active November 18, 2016 04:00
Weak event listeners
var component1 = {
//this method will receive the event and process it
handleEvent: function(event){
//this is only for debugging
console.log("debugging!", event.data);
//the event causes the state of the component to change
@mariusGundersen
mariusGundersen / deadweight.js
Created November 1, 2013 08:49
AMD with named functions
define([], function() {
return function deadWeight() {
var weight = 1000000;
var theWeight = new Array(weight);
for (var i = 0; i < weight; i++)
theWeight[i] = i;
this.getWeight = function() { return theWeight;}
}
});
@mariusGundersen
mariusGundersen / gist:6925246
Last active May 8, 2022 20:38
Programmer collective nouns
@mariusGundersen
mariusGundersen / amdDemo.js
Last active December 19, 2015 02:38
An alternative to AMD modules, using default parameters in ES6
/*
This is how 3 dependencies are defined using AMD.
Notice how when the number of dependencies grows the
distance between the path to the dependency and the
variable it is stored in grows. It quickly becomes
difficult to see which variable corresponds to which
module name.
*/
define(["jQuery", "an/other/module", "knockout"], function($, dep, ko){
@mariusGundersen
mariusGundersen / gist:4705611
Created February 4, 2013 08:25
What does whatIsA return? 7 or 5 (or something else)
var a = 5;
function whatIsA() {
if(a == undefined) {
var a = 7;
}
return a;
}
whatIsA()//is it 5 or 7?