我创建了一个函数,我可以传递toastr和message的类型,虽然我可以通过创建一个大的if else语句来解决这个问题
if (type == 'success') {
toastr.success('Profile URL Copied!');
} else if (type == 'info') {
toastr.info('Profile URL Copied!');
}
等。我可以直接将参数传递给属性
function toasterMsg(type, msg) {
toastr.options = {
"closeButton": false,
"debug": false,
"newestOnTop": false,
"progressBar": false,
"positionClass": "toast-bottom-right",
"preventDuplicates": false,
"onclick": null,
"showDuration": "300",
"hideDuration": "1000",
"timeOut": "5000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
}
toastr. THE TYPE HERE ('Profile URL Copied!');
}
答案 0 :(得分:1)
如果状态和功能名称相同,您可以调用:
toastr[ type ]( 'Profile URL Copied!' );
这是一个小提琴:
function toastr() {
this.success = function(text) {
console.log("success", text);
}
this.info = function(text) {
console.log("info", text);
}
}
var toastr = new toastr;
toastr['info']('Profile URL Copied!');
toastr['success']('Profile URL Copied2!');

答案 1 :(得分:0)
您可以使用type
变量来访问该方法,就像访问任何对象的属性一样:
function toasterMsg(type, msg) {
toastr.options = {
"closeButton": false,
[...]
};
// Check that toastr.[type] is indeed a function
if (toastr[type] && typeof toastr[type] === "function") {
toastr[type]('Profile URL Copied!');
}
}