user1595858
user1595858

Reputation: 3890

Encapsulation in javascript

How to get types.type1["color"] from below? I mean how to write public method to get it.

Config = (function(){

var ids = {
      id1: "id1",
      id2: "id2",
      ....
  }
var types = {
       type1: {
               "color": "red",
               "size": "10"
                ...
              },
        type2: {
               "color": "red",
               "size": "40"
            }
      }
  return {
      getIds: function(id){
            return ids[id];
       }
      getTypes: function(type){
             return types[type];
       }

 }
}();

Upvotes: 0

Views: 103

Answers (2)

Cristian Sanchez
Cristian Sanchez

Reputation: 32117

Config.getTypes("type1").color

Using:

Config = (function(){
    var ids = {
        id1: "id1",
        id2: "id2"
    };

    var types = {
        type1: {
            "color": "red",
            "size": "10"
        },
        type2: {
            "color": "red",
            "size": "40"
        }
    };

    return {
        getIds: function(id){
           return ids[id];
        },
        getTypes: function(type){
           return types[type];
        }
    };
})();

Upvotes: 6

Sunny
Sunny

Reputation: 4809

Try

Config.getTypes("type1")["color"]

Upvotes: 1

Related Questions