简体   繁体   中英

JavaScript Namespace Uncaught Type Error

I've been trying to make my javascript code better by creating a namespace for my code so that there are no global variable/function clashes and also putting everything in the namespace in an anonymous function so everything is private unless explicitly expressed otherwise through the return statement.
Atm, I'm getting a "Uncaught Type Error: object is not a function"

var Namespace = (function() {
    //private variables/functions
    var a;
    var b;
    function priv_func1() {
        //do stuff
    }

    //public variables/functions
    return {
        pub_var1: b,
        pub_func1: function() {
            //do stuff
        }
    };
})();

$(document).ready(function() {
    var myName = new Namespace();
    myName.pub_func1();
}

So when I remove the () at the end of the Namespace definition which turns the function declaration into a function expression, I get no error, but in the examples I've seen they have the () in there, so I'm wondering what's going on.

I also put this at the start of the Namespace definition as well, to correct when the user accidentally omits the new keyword.

if (!(this instanceof Namespace))
        return new Namespace();

EDIT: Also, should I put my document ready function before or after the namespace definition.

It works! You don'thave to use new , because you've not defined a constructor .

$(document).ready(function() {
   Namespace.pub_func1();
});

What you were trying ot do is accomplishded via modules wich create constructor. Let me show you an example from Javascript: Design Patterns

Modules That Create Constructors

[..] but sometimes it's more convenient to create your objects using constructor functions. You can still do that using the module pattern. The only difference is that the immediate function that wraps the module will return a function at the end, and not an object.

Consider the following example of the module pattern that creates a constructor function

MYAPP.utilities.Array:
MYAPP.namespace('MYAPP.utilities.Array');
MYAPP.utilities.Array = (function () {
   // dependencies
   var uobj = MYAPP.utilities.object,
   ulang = MYAPP.utilities.lang,
   // private properties and methods...
   Constr;
   // end var
   // optionally one-time init procedures
   // ...
   // public API -- constructor
   Constr = function (o) {
      this.elements = this.toArray(o);
   };
   // public API -- prototype
   Constr.prototype = {
      constructor: MYAPP.utilities.Array,
      version: "2.0",
      toArray: function (obj) {
         for (var i = 0, a = [], len = obj.length; i < len; i += 1) {
            a[i] = obj[i];
         }
         return a;
      }
   };
   // return the constructor 
   // to be assigned to the new namespace
   return Constr;
}());

The way to use this new constructor will be like so:
var arr = new MYAPP.utilities.Array(obj);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM