0

bobince wrote a nice post how to write "classes" in JavaScript: How to "properly" create a custom object in JavaScript?

As the article says, there are two ways to write classes in JavaScript: - the closure way - the prototype way

But it's also possible to write classes using the module pattern, isn't it? I'm confused as there is so much information about JavaScript and some of them are contradictory.

Example class with module pattern (?):

var MyClass = function () {
    // Private variables
    var firstName;
    var secondName;

    // Public functions
    return {
        getFullName: function () {
            return (this.firstName + " " + this.secondName);
        },

        setFirstName: function (value) {
            this.firstName = value;
        },

        setSecondName: function (value) {
            this.secondName = value;
        },

        getFirstName: function (value) {
            return this.firstName;
        },

        getSecondName: function (value) {
            return this.secondName;
        }
    }
};



describe("test module", function () {

    it("Test instance", function () {
        var myInstance = new MyClass("Michael", "Jackson");

        expect(myInstance).not.toBeNull();
    });

    it("setter and getter", function () {
        var myInstance = new MyClass("Michael", "Jackson");

        myInstance.setFirstName("Michael");
        myInstance.setSecondName("Jackson");

        expect(myInstance.getFirstName()).toBe("Michael");
        expect(myInstance.getSecondName()).toBe("Jackson");
        expect(myInstance.getFullName()).toBe("Michael Jackson");
    });

    it("Test instances", function () {
        var myInstance1 = new MyClass();
        myInstance1.setFirstName("Michael");
        myInstance1.setSecondName("Jackson");

        var myInstance2 = new MyClass();
        myInstance2.setFirstName("Paris");
        myInstance2.setSecondName("Hilton");

        expect(myInstance1.getFullName()).toBe("Michael Jackson");
        expect(myInstance2.getFullName()).toBe("Paris Hilton");

        expect(myInstance1 != myInstance2).toBe(true);
    });
});
Community
  • 1
  • 1
TheAnonymousModeIT
  • 777
  • 1
  • 6
  • 18
  • 1
    That's basically the closure way. – T.J. Crowder Mar 06 '17 at 14:24
  • 2
    `firstName` and `secondName` in `MyClass` were declared but it's never used. – Tân Mar 06 '17 at 14:29
  • why u don't use the new way to create [classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes) introduced with ecmascript 6? – Jorius Mar 06 '17 at 14:29
  • Can you clarify what you mean by the "module pattern"? That seems like a very vague term. I'm also having trouble figuring out what you're getting at. Yes, there are perhaps a dozen or so different ways to have "classes" in JavaScript, though there are commonalities between the different approaches. – JLRishe Mar 06 '17 at 14:30
  • 1
    what does mean *"module pattern"* for you? because that term its very ambiguous – Jorius Mar 06 '17 at 14:30
  • Any answer depends on how you define the term *class*. That itself is interpreted differently by many programmers and programming languages. – Fabian Klötzl Mar 06 '17 at 14:32
  • In the end you have a function that constructs and returns an object. Whether or not it should be considered as a "class" (constructor) depends on its purpose and how it used. – Felix Kling Mar 06 '17 at 14:34
  • @T.J.Crowder thank you. I found the following article: https://javascriptweblog.wordpress.com/2010/04/22/the-module-pattern-in-a-nutshell/ that says the generic form of a module is: function() { //private state //private functions return { //public state //public variables } } My class is following the form. – TheAnonymousModeIT Mar 06 '17 at 14:35
  • @JLRishe please see my comment above (with the link: https://javascriptweblog.wordpress.com/2010/04/22/the-module-pattern-in-a-nutshell/) – TheAnonymousModeIT Mar 06 '17 at 14:37
  • @TheAnonymousMode Ok. Yes, that is one more way to make "classes" though you haven't gotten it quite right because you're using `this`. But like I already asked, what kind of answer are you looking for besides, "Yes, you are right, that is one more way to make 'classes'."? – JLRishe Mar 06 '17 at 14:41
  • @JLRishe I'm trying to figure out how to create "classes" in JavaScript (I'm aware JavaScript is a classless language...) and how it is related to the module pattern. May be the definition of a module which I pasted above is not correct? I've found some other source that says: a module is always an immediately-invoked function expression. If this is correct it's not possible to create instances of it. It's something like a namespace. – TheAnonymousModeIT Mar 06 '17 at 14:49
  • I've played around with the code I've pasted. It makes no sense to expose public functions with the return statement as it's possible to access the "private" members anyway. I get the impression that this code has nothing to do with the module pattern. I've also found a post Eric Miraglia that explains the module pattern: https://yuiblog.com/blog/2007/06/12/module-pattern/ – TheAnonymousModeIT Mar 06 '17 at 15:09

0 Answers0