I think a mixin sugar method is a great idea, but it belongs in a
separate module devoted to those kinds of language extension. (I have
a few methods that I always end up defining, mostly to easily create
callbacks and do currying/binding.)
However, that's not really the issue, since you can do that already.
Let's say, in foo.js, you have this:
exports.foo = function () { puts(this.bar) };
exports.bar = "baz";
So, this works as expected:
var f = require("foo.js");
f.bar = "asdf";
f.foo() // puts("asdf")
However, let's say you have bar.js, like this:
var f = require("foo.js");
for (var i in f) exports[i] = f[i];
If I wanted them to be the *same* object, then that doesn't work. For
instance:
var b = require("bar.js");
var f = require("foo.js");
f.bar = "asdf";
b.foo() // expect "asdf", actual "bar"
What I'm looking for is a way to include one file, and have it sort of
symlink to the exports object of another file. A filesystem symlink
between files won't work, because the relative paths will be messed
up. Also, I'd prefer to make this effect explicit, since it's not
something you'd want to do except in certain situations. (Like, for
instance, if you have a library file at /some/strange/location.js and
want to link it to /packagename.js.)
Another option, of course, is just to set the exports.__proto__.
However, I'm really looking for a reference, not a child, because that
would introduce oddness in hasOwnProperty() checks.
> I'd rather not add to the growing global namespace. Technically this
> isn't global, but practically it is.
That's a very good point.
What about if it was a static function hanging off node or
node.Module? It's not really any more complexity in the code, and it
would probably be beneficial to group setExports in a happier home.
Anyone care to bikeshed on the name? Is it clear what it does? I
also was thinking that just export() might make sense (in that I'm
telling it to "export this thing here"), but that could be confusing.
Looks too much like "exports.foo", and one might be inclined to export
() a bunch of things and expect them to all be made available, when in
actuality, only the last one would win.
--i