11
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?

1
  • Just a piece of advice: the ternary operator in some other languages is not used to write complete statements, but only the expression part (i.e. the part right of the '=' sign). In Java this would fail to compile in the first place. It's better not to make a habit out of this. Commented Jul 4, 2010 at 10:41

6 Answers 6

21

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
8

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?

2 Comments

+1 - I had a similar answer, but mis-read the intention. Yours is right.
@patrick - It took me a while to evaluate the short-circuiting which is always a screaming signal to not try to be clever :)
2
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!

2 Comments

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

i agree with glowcoder but if you still want it:

the_styles ? function(){ the_styles.appendTo('head'); the_styles=null;}() : the_styles = $('.stylesheet').detach();

Comments

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

you dont need to null it if your overwriting it !

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.