Is there any way to simplify such code?

if (isShow) {
  $("-sharpslider").addClass("show")
} else {
  $("-sharpslider").removeClass("show")
}

I don"t know how to simplify


$('- sharpslider') [isShow? "addClass": "removeClass"] ("show")


  • trinocular operation

    !!isShow?$('-sharpslider').addClass('show'):$('-sharpslider').removeClass('show)
  • function wrapper

    let toggle=(element, clazz, condition )=>!!condition?element.addClass(clazz):element.removeClass(clazz)
    
    toggle($('-sharpslider'),'show',isShow)
  • neuropathy IIFF

    ((element, clazz, conditon)=>!!condition?element.addClass(clazz):element.removeClass(clazz))($('-sharpslider'),'show',isShow)
  • final result
    forget it. If you use it frequently, you can also encapsulate a toggle . Other things are unnecessary and can be optimized, such as saving $('spider') to variables.

if the same logic is used in more than one place, the function can be encapsulated.
if there is only one place, there is nothing wrong with not encapsulating it for readability.


jq has a toggle method $('- sharpslider'). ToggleClass ('show', isShow)

)
Menu