views:

171

answers:

8
the_styles ? the_styles.appendTo('head'); the_styles=null : the_styles = $('.stylesheet').detach();

Obviously, this isn't valid. Notice the ";" between the appendTo() and the_styles=null. How do I write it on 1 line and still have multiple expressions like that?

+2  A: 

If you need two lines like that, why not just use an if statement?

glowcoder
Yeah, mcherm showed me the only way, and it's looks nasty, ill go for the if/else
Oscar Godson
Just to add onto that-- ternary shouldn't be used this way it's poor style. It's acceptable if you are going to do something like`print variable ? "1": "2"` but if you're looking into long statements that get complicated, it's much easier for you and a doubly so a programmer after you to figure out what that code does in a few months.
+2  A: 
the_styles ? (function() {the_styles.appendTo('head'); the_styles=null})() : <etc>

Just wrap the code block in (function() { and })().

Now for the hard part: why would you want to do this? Perhaps there's a better solution!

mcherm
yeah, thats ugly, nevermind. I just like ternary's because they're clean and a single line is all.
Oscar Godson
As long as the code in each branch is a single "line" they're nice. Otherwise... use multiple lines!
mcherm
+1  A: 

i agree with glowcoder but if you still want it:

the_styles ? function(){ the_styles.appendTo('head'); the_styles=null;}() : the_styles = $('.stylesheet').detach();
Gregoire
Yeah, i do too now, thanks!
Oscar Godson
A: 
the_styles ? the_styles.appendTo('head') : the_styles = $('.stylesheet').detach();

you dont need to null it if your overwriting it !

RobertPitt
I need the null because if not, it keeps the same value after the initial click and i need an "off/on" function.
Oscar Godson
A: 
the_styles=the_styles || $('.stylesheet').detach(); the_styles.appendTo('head');
galambalazs
Doesnt work... :(
Oscar Godson
A: 

developer
If you want to remove your answer, for whatever reason, just press ‘delete’.
Marcel Korpel
+9  A: 

Use the comma operator this way:

the_styles ? (the_styles.appendTo('head'), the_styles=null) : the_styles =  $('.stylesheet').detach();

Here's what the Mozilla Developer Center writes about the comma operator:

You can use the comma operator when you want to include multiple expressions in a location that requires a single expression. The most common usage of this operator is to supply multiple parameters in a for loop.

Read more here: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/Comma_Operator

Andrea Zilio
That's a really neat form. I also didn't expect the braces to so profoundly change the meaning of the statement. I don't think I'd write this even by accident, but compare `var x = 1,y=2,z=3;` and `var x = (1,y=2,z=3);`. In the first case, `x` is set to `1`. In the second case, `x` is set to `3`. That's really, really neat.
Andrew
+5  A: 

Who needs the ternary operator?

​the_styles = !the_styles && $('.stylesheet').detach()​​​​ ||
             the_styles.appendTo('head') && null;​

Had to switch the expressions around as otherwise the null value of the first expression will always force the second expression .detach() to be evaluated.

The only thing about clever code is that once you come back to it after a coffee break, it won't make any sense even to you. So this is much better:

if(the_styles) {
    the_styles.appendTo('head')
    the_styles = null;
}
else {
    the_styles = the_styles.detach('.stylesheet');
}

To me, even the above simplistic version doesn't make any sense. The what part is obvious, but why is it doing that?

Anurag
+1 - I had a similar answer, but mis-read the intention. Yours is right.
patrick dw
@patrick - It took me a while to evaluate the short-circuiting which is always a screaming signal to not try to be clever :)
Anurag