1

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!

4
  • How many arguments are you passing to searchURL when 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')? Commented Jun 28, 2016 at 15:27
  • The idea was to create a function which could accept a flexible amount of arguments and they should always be strings. Like I mentioned in the update, I want it to also store, and be able to add strings. This is my first attempt at using the module pattern \o/ and wanted to try to extend to be a sort of database... Commented Jun 28, 2016 at 15:35
  • Do you have a code example of how you call it, and what you are expecting back? Are you expecting an array of results back? Commented Jun 28, 2016 at 15:38
  • Actually using Dylon solution worked. However I had some questions which I outlined here and in the update. Hope that makes sense! Commented Jun 28, 2016 at 15:52

3 Answers 3

2

You've incremented i past the last index of the array, try this:

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 - 1]];
            }
      }
 };
Sign up to request clarification or add additional context in comments.

Comments

2

Arguments length is 1 but there is no value at arguments[1]. i is set to 1 by executing the for loop (i++). You only have a value at arguments[0]. When you pass only 1 argument, argument[1] will be undefined, and therefore you cannot find what you are looking for in the object.

Why not just pass in the key you are looking for (like you are already doing), and reference the key from the object in the function?

var outerObj = {

  nestedObj: {
    nestedObjMethod: function(key) {
      var URLS = {
        'url1': 'http://foo.com',
        'url2': 'http://bar.com',
        'url3': 'http://yay.com'
      };
      return URLS[key];
    }
  }
};

console.log(outerObj.nestedObj.nestedObjMethod('url1'));

1 Comment

Thanks for helping me. I have updated question with some context. Maybe you can add some insight with that information?
1

Because after you run your for loop, the value of i is one more than the number of arguments you have so arguments[i] is always beyond the last argument and thus will be undefined.

Your whole for loop is doing nothing so it isn't really making much sense here what you're trying to accomplish with that code.

If you just want to index into the URLs structure with the argument that was passed, then you can just do this:

var outerObj = {

    nestedObj: {
        nestedObjMethod: function(index) {
            var URLS = {
                'url1': 'http://foo.com',
                'url2': 'http://bar.com',
                'url3': 'http://yay.com',
            };
            return URLS[index];
        }
    }
};


outerObj.nestedObj.nestedObjMethod('url1');   // 'http://foo.com'

If you were trying to accomplish something else with your for loop, then please describe what you were trying to accomplish with it.

1 Comment

Thanks jfriend00! I have made an updated with some context. Hopefully that helps, thank you for your insight.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.