Javascript 如何将附加选项合并到jquery函数?

Javascript 如何将附加选项合并到jquery函数?,javascript,jquery,Javascript,Jquery,为了提高jquery/javascript技能,我正在尝试重构和清理一些重复代码。向函数添加附加选项的最佳方法是什么 以Select2为例,我有一个默认函数,偶尔需要添加其他选项 $(".selectable").select2({ theme: "classic" }); $(".selectable .placeholder").select2({ theme: "classic" placeholder: "Select a state", allowClear: tru

为了提高jquery/javascript技能,我正在尝试重构和清理一些重复代码。向函数添加附加选项的最佳方法是什么

以Select2为例,我有一个默认函数,偶尔需要添加其他选项

$(".selectable").select2({
  theme: "classic"
});

$(".selectable .placeholder").select2({
  theme: "classic"
  placeholder: "Select a state",
  allowClear: true
});

$(".selectable .taggable").select2({
  theme: "classic"
  tags: true,
  tokenSeparators: [',', ' ']
})
这有点做作,实际代码要复杂得多,但希望这能说明存在默认条件,并且偶尔会添加额外的选项

我在找像这样的东西

$(".selectable").select2({
  theme: "classic"
});

if $(".selectable .taggable").length
  $(".selectable").merge_options({
  tags: true
  tokenSeparators: [',', ' ']
  })

这可能吗?如果可能,如何实现

询问插件作者。使用
$。扩展