Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructors in the Module Pattern

When using the module pattern in javascript how should constructors be defined, if at all. I would like my constructor to fit into a standard module pattern and not be global.

Why doesn't something like this work, is it complete and total nonsense?

var HOUSE = function() {
    return {
        Person: function() {
            var self = this;
            self.name = "john";
            function name() {
                return self.name;
            }
        }
    };
}();

var me = new HOUSE.Person();
alert(me.name());
like image 364
rogermushroom Avatar asked Jan 19 '12 16:01

rogermushroom


People also ask

What is the constructor pattern?

In classical object-oriented programming languages, a constructor is a special method used to initialize a newly created object once memory has been allocated for it. In JavaScript, as almost everything is an object, we're most often interested in object constructors.

Which is the correct way to define a module pattern?

In software engineering, the module pattern is a design pattern used to implement the concept of software modules, defined by modular programming, in a programming language with incomplete direct support for the concept.

What is the module pattern in JavaScript?

The Module Pattern is one of the important patterns in JavaScript. It is a commonly used Design Pattern which is used to wrap a set of variables and functions together in a single scope. It is used to define objects and specify the variables and the functions that can be accessed from outside the scope of the function.

What are constructors in JavaScript explain with an example?

A constructor is a special function that creates and initializes an object instance of a class. In JavaScript, a constructor gets called when an object is created using the new keyword. The purpose of a constructor is to create a new object and set values for any existing object properties.


2 Answers

Your code is almost fine. However the function name() was not public but the variable was so you were trying to execute the variable causing an error. Add the function getName onto the object and call that instead:

var HOUSE = function() {
    return {
        Person: function() {
            var self = this;
            self.name = "john";
            self.getName = function() {
                return self.name;
            }
        }
    };
}();

var me = new HOUSE.Person();
alert(me.getName());

http://jsfiddle.net/8nSbP/

like image 98
Richard Dalton Avatar answered Sep 30 '22 09:09

Richard Dalton


Using var and function foo() {} (the latter as a declaration, which means "just" function foo() {} without assigning it), create local symbols. So, the function is not available outside the constructor.

Whatever you want to expose (make public), you should assign to this (or self since you defined self = this):

self.getName = function() {
    return self.name;
};

Note that you already used name, so I gave function another name. If you wanted to make the name string local, and expose the function, then they can have the same name since there is no conflict. E.g.:

var name = "john";

self.name = function() {
    return name;
};
like image 28
pimvdb Avatar answered Sep 30 '22 08:09

pimvdb