Search the web
Sign In
New User? Sign Up
jslint_com · JSLint.com
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want to share photos of your group with the world? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Nested ?: operator values.   Message List  
Reply | Forward Message #727 of 993 |
Re: [jslint] Nested ?: operator values.

--- In jslint_com@yahoogroups.com, Michael Lorton <mlorton@...> wrote:
>
> Well, val1 and val2 are the only possible values, you could do this:
>
> var next = {};
> next[val1]=val2;
> next[val2]=val1;
> return this[prop] = next[this[prop]] ;
>
> If the function is to be executed over and over, the first three statement are
only needed at initialization time. Plus, this algorithm generalizes to any
fixed, repeating sequence of values. Say you wanted to loop through 1, 4, 9
over and over (starting at 1 if the property is not already in the sequence):
>
> return this[prop] = { 1: 4, 4: 9, 9: 1} [this[prop]] || 1;
>
> Was this the question or did you want to talk about the ternary operator?
>
> M.
>

I did a little tinkering, and here's what I came up with:

return (this[prop] = ({ val1: val2, val2: val1 })[this[prop]]);

What a fantastic construct. Create an anonymous object, then use it immediately!
Unfortunately this doesn't work, because val1 and val2 are variables that are
determined at run-time, so this will fail.

Nonetheless, you have my gratitude for pointing out a wonderful construct that I
plan to use more often in my code. For now, I'll stick with the ?: operator.




Thu Jul 9, 2009 4:38 pm

pauanyu
Offline Offline
Send Email Send Email

Forward
Message #727 of 993 |
Expand Messages Author Sort by Date

I understand that the ?: operator is supposed to be used to select between multiple values, rather than multiple actions. I ran into an interesting situation:...
pauanyu
Offline Send Email
Jul 9, 2009
3:12 am

Well, val1 and val2 are the only possible values, you could do this: var next = {}; next[val1]=val2; next[val2]=val1; return this[prop] = next[this[prop]] ; If...
Michael Lorton
mlorton
Offline Send Email
Jul 9, 2009
3:42 am

... That seems to be a good solution as well. However, that was not the question. I was indeed talking about the ?: operator, specifically about a JSLint error...
pauanyu
Offline Send Email
Jul 9, 2009
4:10 am

... I did a little tinkering, and here's what I came up with: return (this[prop] = ({ val1: val2, val2: val1 })[this[prop]]); What a fantastic construct....
pauanyu
Offline Send Email
Jul 9, 2009
4:38 pm

Yeah, I did exactly that first too, but { val1 : val2 } is equivalent to { "val1" : val2 }. The braces notation only works if the sequence is expressed as...
Michael Lorton
mlorton
Offline Send Email
Jul 9, 2009
5:41 pm

... The thing that JSLint did not like was the use of an assignment as an expression. It will accept this[prop] = (this[prop] === val1) ? val2 : (this[prop]...
Douglas Crockford
douglascrock...
Offline Send Email
Jul 9, 2009
1:11 pm

... Oh, I see! So this works: return (this[prop] = (this[prop] === val1) ? val2 : (this[prop] === val2) ? val1 : this[prop]); Thanks for your help. Nice to...
pauanyu
Offline Send Email
Jul 9, 2009
3:31 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help