2

I've been reading through some global scare articles, you can thank Crockford for this question.

For instance, I have a function I've defined as:

function httpRequest() {}

This is a pretty generic function name, which could potentially be used in a thirdparty library. Should I just make it a habit to attach the function to an app namespace?

APPNAME.httpRequest() {}

I read through Google's style guide for JavaScript and they recommended this for variables, what about functions and classes?

APPNAME.MyClass = (function () {
  'use strict';

  function MyClass() {}

  MyClass.prototype.myMethod = function () {};

  return MyClass;
}());

EDIT: Let's say I plan on combining with a lot of other libraries.

14
  • please elaborate on some global scare articles - any sources ? Commented Jun 20, 2014 at 21:19
  • 1
    global variables are bad, there are too many articles Commented Jun 20, 2014 at 21:20
  • 1
    yuiblog.com/blog/2008/04/16/global-domination-part-two. I see a lot of libraries on github wrapping everything in an app namespace. Commented Jun 20, 2014 at 21:21
  • 1
    There's no one answer to this question -- it's an opinion-based issue depending on whether you expect to be combining this with other Javascript, etc. Commented Jun 20, 2014 at 21:22
  • 3
    @user3448187 naa, you should just use modules - that way your variables are closure scoped. Look into RequireJS and browserify Commented Jun 20, 2014 at 21:24

1 Answer 1

2

JavaScript variables are closure scoped. So you can always use an IIFE to encapsulate them:

(function(){
    function httpRequest() { /* */ }
    httpRequest(...);
})()

//httpRequest is not defined here unless it was defined above the block

You can also export stuff to the global namespace

var httpUtils = (function(){

    return whatever; // whatever is exported, the other variables are internal
})()

Creating objects that encapsulate behavior like this and exposes some is called a module pattern.

You can use module loaders to manage your modules. One example is RequireJS.

require(["httpUtils","logger",function(http,log){
    http.request(...); // uses from other module
    log(..); // use other module
    return { }; // the return values can be used in other modules, require by file name
                // or explicit configuration
});

Namespacing is also an option, but it is generally less desirable.

Sign up to request clarification or add additional context in comments.

6 Comments

what if I only have on httpRequest method though, should I still make a module httpUtils? I'm reading some module articles right now XD
@user3448187 httpRequest seems like a giant method if that's the only HTTP method you have, however if it is indeed the only method you have and it doesn't belong logically anywhere else (like a REST module etc) I'd put it in its own file (module in this case)
very interesting, I'll continue reading up on this, thanks for all your help!
probably you want to include an example with arguments for your examples. Functions without arguments are the exceptions
@Toskan the (...) is supposed to symbolize arguments :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.