Skip to content

Instantly share code, notes, and snippets.

@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 / demo.js
Created May 2, 2012 21:29
The usefulOrientation function takes the arguments from an DeviceOrientationEvent and returns the orientation of the interface relative to the world
/*
Demo showing how to use the usefulOrientation function.
*/
function onDeviceOrientationChange(e){
console.log("deviceOrientation", e.alpha, e.beta, e.gamma);
var orientation = usefulOrientation(e.alpha, e.beta, e.gamma);
console.log("usefulOrientatino", orientation.alpha, orientation.beta, orientation.gamma);
}
window.addEventListener("deviceorientation", onDeviceOrientationChange, false);
@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){
@mariusGundersen
mariusGundersen / gist:6925246
Last active May 8, 2022 20:38
Programmer collective nouns
@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 / 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 / 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 / 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;