SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Forums Designer
    Join Date
    Jun 2004
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating modular class

    I'm trying to make a class that can be extended by modules. All modules have same functions. When creating instance of class first parameter should be selected module name, like var test = new testClass('module1');

    So far I came up with following code:
    Code:
    // Variable to store modules list
    var testClassModules = {};
    
    // My test class
    function testClass(mode) {
    	this.mode = mode;
    	this._modules = testClassModules;
    	
    	// Sample function
    	this.testFunction = function() {
    		// Do some global stuff here
    		
    		// Call module's function to do module specific stuff
    		this._modules[this.mode].testFunction.call(this);
    	};
    };
    
    // Test module #1
    testClassModules['module1'] = {
    	testFunction: function() { 
    		alert('Module #1. Mode = ' + this.mode); 
    	}
    };
    
    // Another test module
    testClassModules['module2'] = {
    	testFunction: function() { 
    		alert('This is second module. Mode = ' + this.mode); 
    	}
    };
    
    // Creating instance of test class, selecting first module
    var test = new testClass('module1');
    test.testFunction();
    That code works fine, but I don't like creating global variable for modules list. Is there a cleaner way to create similar code?

  2. #2
    Forums Designer
    Join Date
    Jun 2004
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After thinking a bit more, I came up with a different solution that I think is cleaner:
    Code:
    function testClass() {
    	this.mode = '';
    	this.testFunction = function() {
    		// Do some global stuff here
    		this.localTestFunction();
    	};
    
    	// Initialize
    	if (arguments.length > 0)
    	{
    		eval('var test = new testClass' + arguments[0].charAt(0).toUpperCase() + arguments[0].slice(1) + '();');
    		return test;
    	}
    };
    
    // Test module #1
    function testClassModule1() {
    	var that = new testClass();
    	that.mode = 'module1';
    	that.localTestFunction = function() {
    		alert('Module #1. Mode = ' + this.mode); 
    	};
    	return that;
    };
    
    // Test module #2
    function testClassModule2() {
    	var that = new testClass();
    	that.mode = 'module2';
    	that.localTestFunction = function() {
    		alert('This is second module. Mode = ' + this.mode); 
    	};
    	return that;
    };
    
    // Creating instance of test class, selecting first module
    var test = new testClass('module1');
    test.testFunction();
    That code works fine too, but I don't like using eval() to create module specific instance. Is there another way of creating class instance from string without using eval()?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •