最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can I use classes with the javascript module pattern? - Stack Overflow

programmeradmin0浏览0评论

I have a bunch of "modules" which follow the "JavaScript Module Pattern" as described in this popular article. To my understanding these modules are a way to aggregate various bits of behavior into neat namespaces.

But what if I want to be able to create a unique object instance which accepts arguments? As it stands, I can't do that as all the data is shared/static. I want to be able to do this:

var foo = new Some.Namespace.Whatever.Foo(config);

I can't change the structure of the pattern as we've been using it for a while and it works very well. I just want to tweak it so I can throw some "classes" into it which work with non-static data.

I have a bunch of "modules" which follow the "JavaScript Module Pattern" as described in this popular article. To my understanding these modules are a way to aggregate various bits of behavior into neat namespaces.

But what if I want to be able to create a unique object instance which accepts arguments? As it stands, I can't do that as all the data is shared/static. I want to be able to do this:

var foo = new Some.Namespace.Whatever.Foo(config);

I can't change the structure of the pattern as we've been using it for a while and it works very well. I just want to tweak it so I can throw some "classes" into it which work with non-static data.

Share Improve this question asked Mar 6, 2013 at 16:13 Bobby BBobby B 2,3253 gold badges25 silver badges48 bronze badges 3
  • I don't understand what the problem is. Why can't you have a class as a member of a module? – benekastah Commented Mar 6, 2013 at 16:19
  • @benekastah If I do then any data I pass in is shared by other instances when I use new. – Bobby B Commented Mar 6, 2013 at 16:22
  • I don't think so. I added an answer that I should work, if I understand the problem correctly. – benekastah Commented Mar 6, 2013 at 16:28
Add a ment  | 

2 Answers 2

Reset to default 5

Why not try this?

var MODULE = (function () {
  var my = {};

  my.SomeClass = SomeClass;
  function SomeClass(six) {
    this.six = six;
  }
  SomeClass.prototype.five = 5;

  return my;
})();

When you call var obj = new MODULE.SomeClass(6) it will give you a new object. obj.five is shared between all instances of SomeClass because it is attached to the prototype. However, obj.six is specific to that instance, because it is attached to obj.

Alternatively, you may not need for your class to be inside a module. You could use a class to replace the module where appropriate, because both modules and classes in this case have some overlapping functionality.

This is probably overuse of the module pattern, but is this what you mean?:

var Some = (function() {
    var util = { /* ... */ };
    return {
        Namespace: (function() {
            return {
                Whatever: (function() {
                    var Foo = function(config) {
                        this.foo = config.foo || "foo";
                        this.bar = config.bar || "bar";
                    };
                    var Bar = function() {};
                    Foo.prototype.toString = function() {
                        return this.foo + " "  + this.bar;
                    };
                    return {
                        Foo: Foo,
                        Bar: Bar
                    }
                }())
            };
        }())
    };
}());

var foo = new Some.Namespace.Whatever.Foo({foo: "hello", bar: "world"});
foo.toString() // "hello world"
发布评论

评论列表(0)

  1. 暂无评论