Test Class Creation

by cjgaudin

HTML

<div id="resultOutput"></div><div id="resultOutput2"></div>

JavaScript

var TestObject = (function($) {
    //Although this is a private member, it is static to all new
    // instances, because his section is executed once and only once
    // If this wasnt a module pattern and was just an entire function
    // then it would be executed multiple times and would be different per
    // instance
    
    var _testMember = 'testMember';
    
    TestObject = function(name) {
        var _testMember2 = name;
        
        _testMember = name;
        this.name = name;
        this.publicFunc = function() {
            var result = privateFunc();
            return result;
        };        
        this.publicFunc2 = function() {
            return _testMember2;
        };
    };
    
    privateFunc = function() {
        return _testMember;
    };

    return TestObject;
})(jQuery);

var TestObject2 = function(name) {
    var _testMember = name;
    this.name = name;
    
    this.publicFunc = function() {
        return _testMember; 
    }; 
    
    this.publicFunc2 = function() {
        return privateFunc();  
    };
    
    privateFunc = function() {
        return _testMember;  
    };
};


$('#resultOutput').html('Testing here .... ');
var testObject1 = new TestObject('some name1');
var testObject2 = new TestObject('some name2');

$('#resultOutput').html(testObject1.publicFunc() + ' =? ' + testObject1.publicFunc2() + ' =? ' + testObject1.name);

var testObj1 = new TestObject2('name1');
var testObj2 = new TestObject2('name2');

$('#resultOutput2').html(testObj1.publicFunc2() + ' =? ' + testObj1.publicFunc() + ' =? ' + testObj1.name);