tag:blogger.com,1999:blog-34049130.post6233505747147786320..comments2024-03-18T08:24:48.823+01:00Comments on The Tapir's Tale: Javascript ConditionalsAnders Janmyrhttp://www.blogger.com/profile/16081537018884349622noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-34049130.post-151531603265894622013-04-13T11:56:24.440+02:002013-04-13T11:56:24.440+02:00@jefferey, you are write about the falsy values, i...@jefferey, you are write about the falsy values, it is worth pointing out. Thanks!Anders Janmyrhttps://www.blogger.com/profile/16081537018884349622noreply@blogger.comtag:blogger.com,1999:blog-34049130.post-25266884864533990092013-04-12T23:20:39.938+02:002013-04-12T23:20:39.938+02:00@Rhys: Note that
COND && FUNC() || ERROR(...<b>@Rhys</b>: Note that<br /><br />COND && FUNC() || ERROR();<br /><br /><i>demands</i> that FUNC() return a true value. So even if COND is true, if FUNC() returns a false value, ERROR() will be called.<br /><br />COND ? FUNC() : ERROR();<br /><br />gets around that problem.<br /><br />---<br /><br />My comment on the article itself is that it assumes 0 or false is never a valid value to be passed to a function as an argument; it always replaces 0 or false with the default value. I tend to check for null or undefined (based on my needs), or I use the 'arguments' built-in.Jeffrey Pinyanhttps://www.blogger.com/profile/08758581112217835988noreply@blogger.comtag:blogger.com,1999:blog-34049130.post-6273531641799636512013-04-12T20:43:34.426+02:002013-04-12T20:43:34.426+02:00@Golmote, Yes, thats true, you have to beware if i...@Golmote, Yes, thats true, you have to beware if it matters to you.Anders Janmyrhttps://www.blogger.com/profile/16081537018884349622noreply@blogger.comtag:blogger.com,1999:blog-34049130.post-1004523212818613262013-04-12T20:42:27.564+02:002013-04-12T20:42:27.564+02:00@Rhys, Ah, cool, I didn't know that Closure co...@Rhys, Ah, cool, I didn't know that Closure could pick up on that.Anders Janmyrhttps://www.blogger.com/profile/16081537018884349622noreply@blogger.comtag:blogger.com,1999:blog-34049130.post-14818585437256496412013-04-12T17:49:35.005+02:002013-04-12T17:49:35.005+02:00Note that in the callback example, the two functio...Note that in the callback example, the two functions don't have the same return value if callback is falsy (first will return undefined, second will return callback).Golmotehttps://www.blogger.com/profile/15876647273141527988noreply@blogger.comtag:blogger.com,1999:blog-34049130.post-39405669038807075192013-04-12T16:58:46.008+02:002013-04-12T16:58:46.008+02:00ah true - throw doesn't work, not even in a te...ah true - throw doesn't work, not even in a ternary. You could always have a global function that errors for you:<br /><br />err = function(error) {<br />console.log('error in function' + err.callee);<br />throw new Error(error);<br />;};<br /><br />and use that instead to make it work then do:<br /><br />func ? func() : err("invalid kind " + kind);<br /><br />though a better way would probably be to have an assert at the start of your function, then you can just have:<br /><br />assert(kind[kind]);<br />kind[kind]();<br /><br />or if you use closure then you can have an enum with the values, something like:<br /><br />/**<br /> * @enum {string}<br /> */<br />kinds.types = {<br />BAIRD: 'baird',<br />MALAYAN: 'malayan'<br />}<br /><br />then you can just have a check in the jsdoc that should get picked up by the compiler:<br /><br />/**<br /> * @param {kinds.types}<br /> */<br />var runType = function(type) {<br /> kind[type]();<br />}<br /><br />and call it with something like:<br /><br />runType(kinds.types.BAIRD);<br /><br />and any errors will get picked up at compile time.rhysbrettbowenhttps://www.blogger.com/profile/05642824819846878549noreply@blogger.comtag:blogger.com,1999:blog-34049130.post-41008143622361310772013-04-12T16:37:37.944+02:002013-04-12T16:37:37.944+02:00@Rhys, I usually only use the operator syntax when...@Rhys, I usually only use the operator syntax when I actually care about the return values. <br /><br />In the case where I am throwing the Error I don't care about the return value of func().<br /><br />In any case it won't parse :( <br /><br />func && func() || throw new Error('Invalid kind ' + kind);<br />SyntaxError: Unexpected token throwAnders Janmyrhttps://www.blogger.com/profile/16081537018884349622noreply@blogger.comtag:blogger.com,1999:blog-34049130.post-76197056882853796022013-04-12T15:56:00.581+02:002013-04-12T15:56:00.581+02:00just to keep things in line for you
var func = ki...just to keep things in line for you<br /><br />var func = kinds[kind];<br />if (!func)<br /> throw new Error('Invalid kind ' + kind);<br />func();<br /><br />-><br /><br />var func = kinds[kind];<br />func && func() || throw new Error('Invalid kind ' + kind);rhysbrettbowenhttps://www.blogger.com/profile/05642824819846878549noreply@blogger.com