Skip to content

Instantly share code, notes, and snippets.

@hhulkko
Created February 26, 2011 11:48
Show Gist options
  • Save hhulkko/845141 to your computer and use it in GitHub Desktop.
Save hhulkko/845141 to your computer and use it in GitHub Desktop.
Possible workaround for lack of proper namespaces in js
function mixIn(module, target) {
for (var name in module)
target[name] = module[name]
}
BASE = {
styled: function(x) { return '<b>' + x + '</b>' }
}
FOO = (function() {
mixIn(BASE, this)
return {
log: function(x) { console.log(styled(x)) }
}
})()
// Test
with (FOO) {
log('Hello world')
}
FOO.log('coolness')
try {
log("this won't work")
} catch(err) {
console.log(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment