我想创建一个像“MessageBox
”这样的类。在调用 Show()
函数时,我将传递所需的参数。喜欢:
MessageBox.Show(
{
str : "Are you sure?",
onYes : function(){
//do something
},
onNo: function(){
// Do another stuff
}
});
我尝试过的:
var MessageBox = {
Show : function(){ // I stuck here
}
}
假设在演出中 JavaScript confirm()
函数被调用。
最佳答案
应该是这样的:
var MessageBox = {
Show : function(opts){ // I stuck here
var result = confirm(opts.str);
if (result) {
opts.onYes();
} else {
opts.onNo();
}
}
}
关于javascript 将参数作为对象或类似的东西传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16415209/