最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Proper use of a comma in javascript ternary operator - Stack Overflow

programmeradmin4浏览0评论

Rather than use an if else statement, I'm trying to use the ternary operator but have a syntax error somewhere in my statement.

Can someone tell me where I am going wrong?

Statement is:

my_alert(status ? ('Accepted', 'alert-success') : ('Declined', 'alert-info'))
  • my_alert is a function which has 2 parameters.
  • Status just evaluates to true or false.
  • When I pass more than 1 parameter into the above expression, it doesn't like the use of the ma.

In chrome and firefox when the function runs it displays 'alert-success' or 'alert-info'. It misses out the first parameter.

I've looked on stackoverflow for the answer but by all means it's telling me that what i'm doing is correct.

Any help would be great.

Rather than use an if else statement, I'm trying to use the ternary operator but have a syntax error somewhere in my statement.

Can someone tell me where I am going wrong?

Statement is:

my_alert(status ? ('Accepted', 'alert-success') : ('Declined', 'alert-info'))
  • my_alert is a function which has 2 parameters.
  • Status just evaluates to true or false.
  • When I pass more than 1 parameter into the above expression, it doesn't like the use of the ma.

In chrome and firefox when the function runs it displays 'alert-success' or 'alert-info'. It misses out the first parameter.

I've looked on stackoverflow for the answer but by all means it's telling me that what i'm doing is correct.

Any help would be great.

Share Improve this question edited Feb 7, 2013 at 18:50 Andreas 21.9k7 gold badges51 silver badges58 bronze badges asked Feb 7, 2013 at 18:48 EddieEddie 6214 gold badges13 silver badges23 bronze badges 1
  • What is the property fingerprint of your my_alert() function? I think you should pass an array of arguments to it instead of wrapping your arguments in () – Robin van Baalen Commented Feb 7, 2013 at 18:51
Add a ment  | 

4 Answers 4

Reset to default 12

Well, the ma operator does the following:

The ma operator evaluates both of its operands (from left to right) and returns the value of the second operand.

That means, ('Accepted', 'alert-success') evaluates to 'alert-success' (as you already noticed). The ma here is different than the ma that separates function arguments. You cannot use it to pass two arguments to a function.

What you can do is store both arguments in an array and use .apply to pass them to the function:

// this is not the ma operator either, this is array literal syntax.
var args = status ? ['Accepted', 'alert-success'] : ['Declined', 'alert-info'];
my_alert.apply(null, args);

I don't think ternary operators can be used to control two values like that:

How about separating them:

my_alert(($status?"Accepted":"Declined"),($status?"alert-success":"alert-info"));

Alternatively, you could just wrap the function call in the ternary statement...

status ? my_alert("Accepted", "alert-success") : my_alert("Declined", "alert-info");

UPDATE:

Robin van Baalen makes a good suggestion...

my_alert.apply(this, status ? ["Accepted", "alert-success"] : ["Declined", "alert-info"]);

You can't use the ma like that. If you want to pass 2 parameters, you need to use 2 ternary statements.

my_alert((status ? 'Accepted' : 'Declined'), (status ? 'alert-success' : 'alert-info'));

In your case, the ma is read a the ma operator, which evaluates both operands and returns the last one. So, your ternary statement was equivalent to:

my_alert(status ? 'alert-success' : 'alert-info')
发布评论

评论列表(0)

  1. 暂无评论