Skip to content

Instantly share code, notes, and snippets.

@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";

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 / 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";
}
@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);
});
function Lazy(list, ...steps){
this.list = list;
this.steps = steps;
}
Lazy.prototype.map = function(f){
return new Lazy(this.list, ...this.steps, iteration
=> ( for (entry of iteration) f(entry)));
}
function toHex(rgb){
var a = /rgb\((\d+),\s*(\d+),\s*(\d+)\)/.exec(rgb).reverse();
a.pop();
return '#'+a.map(a => parseInt(a, 10).toString(16)).join('');
}
var variables = {
'#747474': '#FF0000',
'#f8b912': '#00FFBB'
};
@mariusGundersen
mariusGundersen / expose.js
Created March 8, 2012 08:47
Expose makes it simple to expose functions and properties of AMD modules
define("expose", [], function(){
function toType(obj){
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}
return function(){
var list = arguments;
var func = /^function\s+([^(]+)/i;
var i=0;
var obj;
@mariusGundersen
mariusGundersen / AsyncQueue.js
Last active November 9, 2015 21:39
AsyncQueue example
export default class AsyncQueue {
constructor() {
this.items = [];
this.receivers = [];
}
push(value) {
if(this.receivers.length == 0){
this.items.push(value);
}else{
@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?
@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){