I wasn't sure how to title this question, but I'm using the revealing module pattern with constructors and noticed some behavior that confused me. The actual purpose of this code is irrelevant to my question I just want to understand this behavior I saw.
Here is the code that I had issues with:
var ajaxModule = (function() {
//private
var data,callBack,type,dataType,url,
//public
constructor;
constructor = function(callBack,data,type,dataType,url) {
//Required
callBack = callBack || null;
data = data || null;
//Defaults
type = type || 'POST';
dataType = dataType || 'json';
url = url || 'DBConnect.php';
};
//methods
function setAjaxQuery() {
log('in setAjaxQuery')
dir(callBack);
dir(data);
dir(type);
dir(dataType);
dir(url);
};
constructor.prototype = {
constructor: ajaxModule,
setAjaxQuery: setAjaxQuery,
};
return constructor;
}());
Now calling it:
var ajax = new ajaxModule('someObject','someData');
ajax.setAjaxQuery();
Now doing this I was seeing undefined for all of those console.dir() commands in the setAjaxQuery function.
I found the problem was that the parameter names were the same as the local variables. I resolved the issue by doing something like this:
constructor = function(zcallBack,zdata,ztype,zdataType,zurl) {
//Required
callBack = zcallBack || null;
data = zdata || null;
//Defaults
type = ztype || 'POST';
dataType = zdataType || 'json';
url = zurl || 'DBConnect.php';
}
My question though is why does this happen? What is javascript doing that causes this problem when the names are the same?