javascript - wrap if else statement as function -
suppose have following if else statement code.
if (checkvalue(3)) { dofunction(); } else { alert('fail'); } function checkvalue(value) { return value; }
and want convert following return same result.
checkvalue(3, dofunction());
how can done?
note: not want repeat if else
because use multiple times in code. neither want use ternary operator
use callback this
function checkvalue(value, cb) { if (value) { cb(value); } else { alert('fail') } } checkvalue(3, dofunction);
note: don't pass dofunction()
, want pass function, not result of calling function
Comments
Post a Comment