Q:

New module inside a module in javascript

Here is my module:    
MyModule = (function () {

    var myModule = function MyModule(strInput) {
        if (false === (this instanceof MyModule)) {
            return new MyModule();
        }

        str = strInput;
    }

    var str;
    myModule.prototype.getStr = function () {
        return str;
    }

    myModule.prototype.test = function () {
        var testModule = new MyModule("testInside");
        console.log(testModule.getStr());
        console.log(str);
    }

    return myModule;
}());

Here is how I test my module:
document.getElementById("Button").onclick = function () {    
        var module = new MyModule("input");    
        module.test();    
    }

This code returns:    
testInside   
testInside 

to the console.log, but what I expect is:    
testInside    
input    

like I do in c# or java when I build a new class inside the same class.

A:

If you want to keep the str private, you can make it like this:
MyModule = (function () {

    var myModule = function MyModule(strInput) {
        if (false === (this instanceof MyModule)) {
            return new MyModule();
        }

        var str = strInput;

        this.getStr = function () {
            return str;
        };

        this.test = function () {
            var testModule = new MyModule("testInside");
            console.log(testModule.getStr());
            console.log(str);
        };
    };

    return myModule;
}());

In this way the str is in the inner closure, so it's like an instance/object variable in other languages. If you put it in the outer closure it will act like a static/class variable. In both cases, the methods of the myModule object still can access it, but it's not visible from the outside.

