I am wondering why I am getting:
outerObj.nestedObj.nestedObjMethod('url1'); // returns undefined
instead of http://foo.com
What am I missing?
Thanks in advance!
var outerObj = {
nestedObj : {
nestedObjMethod : function() {
for (var i = 0; i < arguments.length; i++) {
var URLS = {
'url1': 'http://foo.com',
'url2': 'http://bar.com',
'url3': 'http://yay.com',
};
}
return URLS[arguments[i]];
}
}
};
UPDATE There have been suggestions to expand upon my code because it was hard to tell why I was doing the above.
Below is the context; I had a created a module trying to make use of the revealing module pattern. This is the original code:
var MarkUpChecker = (function iffe() {
'use strict';
var publicAPI = {
getURL: function() {
for (var i = 0; i < arguments.length; i++) {
return {
'url': 'http://url1.com',
'url1': 'http://url2.com',
'url2': 'http://url3.com',
'url3': 'http://www.url4.com',
'url4': 'http://www2.url5.com'
}[arguments[i]];
}
},
searchURL: function() {
var link, url, parser, newPathName = '',
newstr = '';
var container = document.getElementById('container').getElementsByTagName('a');
for (var i = 0, len = arguments.length; i < len; i++) {
url = this.getURL(arguments[i]);
for (var j = 0, jlen = container.length; j < jlen; j++) {
link = container[j];
if (link.href.indexOf(url) !== -1) {
parser = document.createElement('a');
parser.href = link.href;
link.setAttribute('target', '_self');
newPathName = parser.pathname;
if (newPathName.search(/Executive|District|Division|National/) != -1) {
newPathName = newPathName.split('/').pop();
newstr = newPathName;
} else {
newstr = newPathName;
}
link.href = newstr;
} else {
link.setAttribute('target', '_blank');
}
}
}
}
};
return publicAPI;
})();
As you can see this method returns a anonymous object, which I am using as a switch statement and a place to store values.
getURL: function() {
for (var i = 0; i < arguments.length; i++) {
return {
'url': 'http://url1.com',
'url1': 'http://url2.com',
'url2': 'http://url3.com',
'url3': 'http://www.url4.com',
'url4': 'http://www2.url5.com'
}[arguments[i]];
}
},
But afterwards I wondered if I could refactor and eventually get the module to allow one to add urls...I'll be glad to add anymore information! So hopefully it's clear now I nested the object in the method in a object like I did!
searchURLwhen you call it? Do you ever mass more than 1? And what are you passing as arguments to that function? Are you doing something like this:MarkUpChecker.searchURL('url1', 'url2')?\o/and wanted to try to extend to be a sort of database...