0

「 」のようなクラスを作りたいですMessageBoxShow()関数を呼び出すときに、必要なパラメーターを渡します。お気に入り:

MessageBox.Show(
{
  str : "Are you sure?",
  onYes : function(){
   //do something
  },
  onNo: function(){
   // Do another stuff
  }
});

私が試したこと:

var MessageBox = {
  Show : function(){ // I stuck here
   }
}

ショー内で JavaScriptconfirm()関数が呼び出されているとします。

4

5 に答える 5

1

パラメータとしてオブジェクトを渡すだけです:

Show: function(obj) {
  var str = obj.str;
  ...
}
于 2013-05-07T09:09:34.833 に答える
1

あなたはそれを渡すことができます

var MessageBox = {
  Show : function(params){ // I stuck here
    console.log(params.str); //would give you "Are you sure?"
 }
}
于 2013-05-07T09:10:09.403 に答える
1

次のようになります。

var MessageBox = {
  Show : function(opts){ // I stuck here
      var result = confirm(opts.str);
      if (result) {
        opts.onYes();
      } else {
        opts.onNo();
      }
   }
}
于 2013-05-07T09:10:36.627 に答える
1

次のようなことを試してください:

var MessageBox = function() {
    var str = 'put your private variables here';
};

MessageBox.prototype.Show = function(arg) {
    console.log(arg);
};
于 2013-05-07T09:10:42.833 に答える
0

この種のことは、以下のように行うことができます (onYes および onNo 関数がオプションになるように、少しチェックを行います:

var MessageBox = {
    Show: function(args) {
        var answer = confirm(args.str);
        if ((answer) && (typeof args.onYes === "function")) {
            args.onYes();
        }
        else if (typeof args.onNo === "function") {
            args.onNo();
        }
    }
 };

その後、必要に応じて使用できます。

MessageBox.Show({
   str: "Are you sure?",
   onYes: function(){
    //do something
   },
   onNo: function(){
      // Do another stuff
   }
});
于 2013-05-07T09:17:57.477 に答える