简体   繁体   English

JavaScript命名空间未捕获类型错误

[英]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. 我一直在努力通过为我的代码创建一个命名空间来使我的javascript代码变得更好,这样就没有全局变量/函数碰撞,并且还将所有内容都放在匿名函数中,所以除非通过返回明确表达,否则一切都是私有的声明。
Atm, I'm getting a "Uncaught Type Error: object is not a function" Atm,我得到一个“Uncaught Type Error:object is not 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. 我也将它放在Namespace定义的开头,以便在用户意外省略new关键字时进行更正。

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 . 你没有使用new ,因为你没有定义constructor

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

What you were trying ot do is accomplishded via modules wich create constructor. 你正在尝试做的是通过创建构造函数的modules来完成的。 Let me show you an example from Javascript: Design Patterns 让我向您展示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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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