4

I want to create my own namespace, something like $ for JQuery

 (function(app) {
     app.fn.log = function(data) {
          console.log(data);
        };
 }(app));

 app.log("data");

But this code returns an error - Uncaught ReferenceError: app is not defined

How do I create my own namespace?

5
  • Well, where's app defined? The function takes an app argument and you pass an app variable, that doesn't exist... Commented Feb 27, 2014 at 8:19
  • I do not know how and where should I implant that Commented Feb 27, 2014 at 8:20
  • See if this helps addyosmani.com/resources/essentialjsdesignpatterns/book/… Commented Feb 27, 2014 at 8:23
  • I think you must read books about javascript, codeacademy )) Commented Feb 27, 2014 at 8:24
  • I can define that with {} , and it works well, but I can not use just custum namespace Commented Feb 27, 2014 at 8:39

3 Answers 3

3

A common approach for implementing namespacing in javascript is to leverage the module pattern with immediate functions for scoping (like you demonstrated in your question). This also allows you to pass in objects with non-conflicting names and use them with parameterized names that you would prefer such as $ like so:

//Global script declaration
var NAMESPACE = {};

//Check if you need to initialize your namespace
NAMESPACE.FooSpace = NAMESPACE.FooSpace || {};

//Some other script
NAMESPACE.FooSpace = (function($) {
    function Foo1() {
    }

    function Foo2() {
        return $.getSomething(); //some existing function inside of someObjectWithANonConflictingName object
    }    

    return { Foo1: Foo1, Foo2: Foo2 };
})(someObjectWithANonConflictingName);

//somewhere else in code (call as a constructor function)
var foo = new NAMESPACE.FooSpace.Foo1();
//somewhere else in code (call as regular function)
var foo2 = Foo2();
  • This approach will help prevent naming conflicts because you can simply add to the NAMESPACE object in order to have overlapping names of functions and variables.
  • It also gives you the added benefit of being able to renamed variables or functions to something that would otherwise conflict with popular libraries.
  • In addition, this helps really cut down on global namespace because it all is held within the NAMESPACE object.
  • It affords you an alternative option to attaching something to the window object (some find this distasteful).
Sign up to request clarification or add additional context in comments.

Comments

2
window.app = {};

(function(app) {
    app.log = function(data) {
         console.log(data);
       };
}(app));

app.log("data");

jQuery deals a little bit different with it's fn: as you use $('selector') syntax to find elements you need to process jQuery returns spetial wrapper that contains all the fn.func-like definitions you made in addition to the built-in functions.

So if you need to implement some kind of extensibility you should think over such wrapping first. For example:

window.app = function(p) {

    // do whatever you need

    var wrapper = {
        // standard common fields
    };

    if (app.fn) {
        // extend wrapper with app.fn
    }

    return wrapper;
};

By the way what problem are you trying to solve?

In your case there is of course an exception because app in not defined by the time anonymous function gets executed.

3 Comments

thanks, yes it works well now, but I hope you know my other question, as you can see console.log(data); shows the line in browser that this function called, is there any way to pass show the line that was called by function ? I mean show line that I call app.log('data');
What means line? Line number?
It is not possible unfortunately.
1

Your syntax is ok, but you have to define an app. You are passing the app as parameter to your IIFE, but app is not defined! If you wanted to access jQuery as app, simply call your IIFE with jQuery object like this:

(function(app) {
     app.fn.log = function(data) {
          console.log(data);
        };
 }($)); // where $ is your jQuery.

and

$.log("data");

Beware! The log function will be available inside and outside the scope of IIFE!

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.