gpt4 book ai didi

javascript - 将参数设置为方法的属性

转载 作者:行者123 更新时间:2023-12-03 02:44:05 24 4
gpt4 key购买 nike

我创建了一个函数,可以传入 toastr 的类型和消息,尽管我可以通过创建一个大的 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!');

}

最佳答案

如果你的状态和函数名称相同,你可以这样调用:

toastr[ type ]( 'Profile URL Copied!' );

这是一个 fiddle :

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!');

关于javascript - 将参数设置为方法的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48180324/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com