开发者

Method that passes options to its object

In JavaScript I'd like to take an object and pass a parameter to it and based on the parameter have a method that will pass settings to the parameter:

functionName( parameter , settings );

Or

functionName( parameter ).methodName( settings );

example: for my iPhone web app setup I do heavy('setup开发者_运维技巧',{ icon: 'icon.png' }); and I want to move the second parameter to a method: heavy('setup').settings({ icon: 'icon.png' }); for my ease and learning...


Do you mean something like this?

heavy = function() {
  var _settings;

  return {
    setup: function (settings){
      this._settings = settings;
    },

    getSetting: function (settingName) {
      if(this._settings[settingName])
      {              
        return this._settings[settingName];
      }
      return "Setting not found";
    }
  };
}();

heavy.setup({"icon":"myicon.png"});
alert(heavy.getSetting("icon"));


Well you have not given any decent details on what exactly you want to do , but from what I understand you need to return a common object (In most cases a class). The object that is returned needs to implement the method that you want to call as well.

The simplest example I can think of is the jQuery library , they use a very similiar model


maybe you mean something like this:

function lite(type){
  heavy(type, { icon: 'ico.png' });
}

//lite('setup')

or something like this

function getHeavy(obj){
  return function(type){
     heavy(type, obj);
  }
};

var icoFn = getHeavy({icon:'ico.png'});
icoFn('setup');
var poopFn = getHeavy({icon:'poop.png'});
poopFn('setup');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜