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

发布于 2024-12-04 06:30:24 字数 805 浏览 3 评论 0原文

我一直在尝试通过为我的代码创建一个命名空间来使我的javascript代码更好,这样就不会有全局变量/函数冲突,并将命名空间中的所有内容都放在匿名函数中,这样一切都是私有的,除非通过返回明确表达陈述。
Atm,我收到“未捕获类型错误:对象不是函数”

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();
}

因此,当我删除命名空间定义末尾的 ()(将函数声明转换为函数表达式)时,我没有收到任何错误,但在示例中我看到他们有 () 在那里,所以我想知道发生了什么。

我还将其放在命名空间定义的开头,以便在用户意外省略 new 关键字时进行纠正。

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

编辑:另外,我应该将文档就绪函数放在命名空间定义之前还是之后。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烦人精 2024-12-11 06:30:24

有用!您不必使用new,因为您还没有定义构造函数

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

您尝试做的事情是通过创建构造函数的模块来完成的。
中的示例

让我向您展示Javascript:设计模式创建构造函数的模块

[..] 但有时它是
使用构造函数更方便地创建对象。你仍然可以做
使用模块模式。唯一的区别是立即函数
包装模块将在最后返回一个函数,而不是一个对象。

考虑以下创建构造函数的模块模式示例

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文