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)

)
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bf6d08-3187c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bf6d08-3187c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?