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...
Real people. Real stories. See how Yahoo! Groups impacts members worldwide.

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 #726 of 993 |
Re: Nested ?: operator values.

--- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
>
> 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] === val2) ? val1 : this[prop];
> return this[prop];
>
> or
>
> return (this[prop] = (this[prop] === val1) ? val2 :
> (this[prop] === val2) ? val1 : this[pro
>

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 know that JSLint handles nested ?: operators.




Thu Jul 9, 2009 3:30 pm

pauanyu
Offline Offline
Send Email Send Email

Forward
Message #726 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
Online Now 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
Online Now 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