This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var module = (function create( | |
namespace, handler, module, Proxy | |
) { | |
// ------------------------------------ | |
// (C) WebReflection - MitStyle License | |
// ------------------------------------ | |
// @dependency npm install node-proxy | |
// ------------------------------------ | |
// var sys = module.sys; | |
// sys.print("it works!"); | |
// var yet = sys.nonexisting.yet; | |
// yet.doStuff(); | |
// ------------------------------------ | |
return Proxy.create({ | |
get: function (receiver, name) { | |
if (!module.hasOwnProperty(name)) { | |
var key = namespace ? | |
namespace + "/" + name : | |
name | |
; | |
module[name] = require(key); | |
handler[name] = create(key, {}, module[name], Proxy); | |
return handler[name]; | |
} else { | |
return module[name]; | |
} | |
} | |
}); | |
}("", {}, global, global.Proxy || require("node-proxy"))); |
The aim of above snippet is to forget the usage of require ... here some example:
module.sys.puts("Hello implicit require");
var fs = module.fs;
fs.stat( ... );
It's compatible with nested namespaces too and if there are non JS chars in the middle ... well:
var Proxy = module["node-proxy"];
1 comment:
Clever but doesn't actually save any typing...
Post a Comment