1

I want to make a sort of "shopping list" in JS. I'm having trouble accessing the object with the argument of a function.

shopList = {
  create: function createList(listName) {
    listName = {};
    listName["Banana"] = 2
  },

  output: function output(listName) {
    console.log(listName)
  },

};

shopList.create('list')
shopList.output('list')

When I run shopList.output 'list' is returned. How do I access the object over the argument? Hope this isn't a duplicate, been googling for hours now and didn't make any progress.

6
  • 'over the argument'? Commented Oct 17, 2015 at 19:11
  • Sorry english isn't my mother tongue. Like access it with the passed arguement. Commented Oct 17, 2015 at 19:12
  • It might be better with an example output of what you want. Commented Oct 17, 2015 at 19:14
  • I just want to return the entire object in the terminal, so in this case 'banana: 2' should be returned Commented Oct 17, 2015 at 19:15
  • var shopList = { create: function createList(listName) { this.listName = {}; this.listName["Banana"] = 2 }, output: function output(listName) { console.log(this.listName) }, }; shopList.create('list'); shopList.output('list'); Commented Oct 17, 2015 at 19:19

2 Answers 2

5

If you want to be able to configure lists with dynamic names then you need to use bracket notation to create lists within this object. Like this:

var shopList = {
    create: function createList(listName) {
        this[listName] = {};
        this[listName]["Banana"] = 2
    },

    output: function output(listName) {
        return this[listName];
    }
};

shopList.create('list');
console.log(shopList.output('list'));

However, as pointed by @armchairdj in comments, it's better to create a container dedicated property to hold lists:

var shopList = {
    lists: {},

    create: function createList(listName) {
        this.lists[listName] = {};
        this.lists[listName]["Banana"] = 2
    },

    output: function output(listName) {
        return this.lists[listName];
    }
};
Sign up to request clarification or add additional context in comments.

4 Comments

The 2nd console.log() isn't necessary since output() already logs it rather than returning.
@JonathanLonowski I think it makes sense to return, so not it does :)
That's a little naive. shopList.create('output') would break your object.
@armchairdj I know, it's just OP's is creating very simple shopping cart. But you are right.
2

If you want to avoid clobbering the methods of your showList object by naively adding arbitrary, user-supplied keys, you should create an internal namespace for your lists.

var shopList = {
  lists:  {},

  create: function (listName) {
    this.lists[listName] = this.lists[listName] || {};

    this.lists[listName].Banana = 2;
  },

  output: function (listName) {
    var list = this.lists[listName];

    if (!list) {
      return console.log('No list named ' + listName + ' found.');
    }

    console.log(list)
  }
};

shopList.create('output');
shopList.output('output');
shopList.create('foo');
shopList.output('foo');
shopList.output('bar');

Comments

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.