jakobmattsson / z-core

Utility library for JavaScript promises
MIT License
42 stars 0 forks source link

Namespaces for mixins #13

Open osuushi opened 10 years ago

osuushi commented 10 years ago

It seems like name collisions are already a problem for existing mixins, and I feel like this problem can only get worse over time. I propose that it would be a good idea to put the mixins in modules with their own namespaces. So for example, instead of

x.logarithm().log()

you'd have

x.math.log().console.log()

It's a little more typing, but I feel like it would really improve trust to know that adding a mixin library wasn't significantly likely to cause collisions.

jakobmattsson commented 10 years ago

I don't really like the way it reads (x.math.log() is not like anything anyone's used to already), but I see the need for something along these lines. Good thing to raise a discussion around!

Not that it's beautiful, but C-style namespacing would probably make the code easier to understand in my opinion. Maybe x.mathLog().consoleLog() or x.mtLog().conLog() or x.math_log().console_log().

In any case, my suggestion would be that you supply an (optional) namespace when mixing in a module, like this:

Z.mixin({
  log: function() { ... },
  sqrt: function() { ... }
}, "math")

Which one of the namespace syntaxes to produce from it should probably be investigated a bit first! I'll make a poll or something.

If the namespace-parameter to Z.mixin is not supplied, it will apply them globally, like today. Nice for preserving backwards compatibility and for the cases where you only use a few functions/libraries and namespaces do more harm than good.

What do you think?

osuushi commented 10 years ago

That seems like a decent way to do it. If you're going the configurable route, then you could also give an option for the namespace syntax and let devs decide if they want dot or C namespaces.