Skip to content

Namespacing #1141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jscriptcoder opened this issue Feb 17, 2011 · 3 comments
Closed

Namespacing #1141

jscriptcoder opened this issue Feb 17, 2011 · 3 comments

Comments

@jscriptcoder
Copy link

Hi there,

Another idea I have in mind, implementing some namespacing system, using maybe "namespace" or "package" word, something like

CoffeeScript:

namespace com.package.subpackage

JavaScript output:

var __namespace = function (strns) {
    var root, ns, len, _i;
    if (typeof strns === 'string') {
        root = window;
        ns = strns.split('.');
        len = ns.length;
        for (_i = 0; _i < len; _i++) {
            root = root[ns[_i]] = root[ns[_i]] || {};
        }
    }
};

__namespace('com.package.subpackage');

Makes sense ?

Thanks

Fran

@jashkenas
Copy link
Owner

Thanks for the idea, but JavaScript already has nice namespaces in the form of object literals, and there's no need to introduce special Java-esque syntax for it. In addition, the golden rule of CoffeeScript is "no special functions" ... so the proposed implementation wouldn't pass muster.

@jscriptcoder
Copy link
Author

I just though it might be a good feature if CoffeeScript did that for you, instead of using the function above to create namespaces or manually doing something like:

var com = com || {};
com.package = com.package || {};
com.package.subpackage = com.package.subpackage || {};

which is annoying, we could use that helper, "__namespace" (borrowed from YUI), in the same way as "__extends" helper is being used to achieve inheritance. But if no special functions is one of the golden rules of CoffeeScript, fair enough ;-)

Thanks.

@StanAngeloff
Copy link
Contributor

Bake it yourself: https://gist.github.com/731816

namespace 'com.package.subpackage', (exports) ->
  exports.fn = ->

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants