Добрый день,
Написал скрипт, хотел бы, чтобы его прокомментировали. Суть в том, что у меня по таймеру срабатывает запрос к серверу. За это отвечает определенный модуль. Чего я хотел достичь:
1.  Конфигурация параметров к вызову $.ajax хранить  в модуле и просто перевать их во время вызова.
2. Вынести функцию - обработчик респонса во внешнюю функцию. Нигде не мог найти примера.
3. Особенно хотел бы попросить обратить внимание на вызов метода run(), отправку запроса - sendRequest() и обработку респонса - successfulResponceProcess().
С удовольствием выслушал бы дельные рекомендации.
	
	| 
		 Код: 
	 | 
	/* 
 * MyModule.js:
 * This file defines a MyModule.js class to process synchronization 
 */
/*
 * Define constructor:
 */
function MyModule() {
	// Define fields:
	var interval = 10000; // 10 seconds	
	var requestType = "GET";
	var requestDataType = "html";
	var servletUrl = "test/somepattern";
	
	//Define getters for fields:
	this.getRequestType = function(){
		return requestType;
	}
	this.getRequestDataType = function(){
		return requestDataType;
	}
	this.getServletUrl = function(){
		return servletUrl;
	}
	// set interval call of run method:
	var thus = this;
	var runMethodClosure = function() { 
		thus.run(); 
	}
	
	/*
	 * This method processes response
	 */
	this.successfulResponceProcess = function(html){
	 	 ...
	 	 //if needs access to instance method, acces them as:
		thus.instanseMethod();
		...
	}
	
	// Initialization code:
	var timoutCallId = setInterval(runMethodClosure, interval);
	
	//debug method to finish initialization:
	this.finish = function(){
		clearTimeout(timoutCallId);
	}
}
/*
 * run method - is called periodically, 
 * used as template pattern
 * you can call isntance methods here that you plan to call 
 * before/after async request 
 * 
 */
MyModule.prototype.run = function() {
        //access instance methods:
        var someExtraData = this.someInstanceMethod();
        //PAY ATTENTION, in order to access instance methods in $.ajax
        //I've added the second argument to the current instance!
	this.sendRequest(someExtraData, this);
	//some post request calls(but not post responce!)
	this.somePostResponceInstanceMethod();
};
/*
 * sendRequest() method sends request
 * 
 */
MyModule.prototype.sendRequest= function(someExtraData, myModuleInstance) {
		$.ajax({
			type: myModuleInstance.getVersionServletRequestType(),
			dataType: myModuleInstance.getVersionServletRequestDataType(),
			url: myModuleInstance.getVersionServletUrl(),
			data: someExtraData,
			success: myModuleInstance.successfulResponceProcess,
			error: function(xhr, textStatus, errorThrown) {
				alert('An error occurred! ' + ( errorThrown ? errorThrown : xhr.status ));
			}
		});
}; | 
	
PS: я тут ссылаюсь на instance methods. Под ними я понимаю методы, которые определяются либо как:
	
	| 
		 Код: 
	 | 
	function MyClass(){
    this.myInstanceMethod = function(){
        ...
    }
} | 
	
Либо через протипы:
	
	| 
		 Код: 
	 | 
	MyClass.prototype.instanceMethodViaPrototype = function(){
        ...
} |