javascript - Consolidate Similar Functions DRY -
i have these 2 functions similar. thought using strings , eval('man')
, love avoid this, if possible. how can abstract two, it's 1 function?
function showman() { widget.classlist.add(toggle); fx.wipeout({ node: calwoman }).play(); fx.wipeout({ node: pointswoman }).play(); pointsman.classlist.remove(toggle); fx.wipein({ node: calman }).play(); fx.wipein({ node: pointsman }).play(); mtfields(); inputman.focus(); } function showwoman() { widget.classlist.add(toggle); fx.wipeout({ node: calman }).play(); fx.wipeout({ node: pointsman }).play(); pointswoman.classlist.remove(toggle); fx.wipein({ node: calwoman }).play(); fx.wipein({ node: pointswoman }).play(); mtfields(); inputwoman.focus(); } /** @todo abstract , create 1 function - showperson('man') */
it'd easier if consolidate variables object.
function showperson(sex) { var people = { woman: { cal: calwoman, points: pointswoman, input: inputwoman }, man: { cal: calman, points: pointsman, input: inputman } }; sex = sex.tolowercase(); var othersex = sex === 'man' ? 'woman' : 'man'; var show = people[sex]; var hide = people[othersex] widget.classlist.add(toggle); fx.wipeout({ node: hide.cal }).play(); fx.wipeout({ node: hide.points }).play(); show.points.classlist.remove(toggle); fx.wipein({ node: show.cal }).play(); fx.wipein({ node: show.points }).play(); mtfields(); show.input.focus(); } // usage showperson('man'); showperson('woman');
Comments
Post a Comment