在javascript中封装

use*_*858 0 javascript

如何types.type1["color"]从下面获得?我的意思是如何编写公共方法来获取它.

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];
       }

 }
}();
Run Code Online (Sandbox Code Playgroud)

Cri*_*hez 6

Config.getTypes("type1").color
Run Code Online (Sandbox Code Playgroud)

使用:

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];
        }
    };
})();
Run Code Online (Sandbox Code Playgroud)