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

Conditionally apply a ! to a method in JavaScript - Stack Overflow

programmeradmin2浏览0评论

Not sure how better to word this (and, consequently, couldn't find previous answers, though I expect this has been answered before), but I'm interested in whether there's a way to turn code like this:

if ( this.props.mailboxFilter == 'sent' ) {
  return this.currentUser.canActOnBehalfOf( m.senderID );
} else {
  return !this.currentUser.canActOnBehalfOf( m.senderID );
}

To something like the below (not sure how better to express it):

var bangOrNot = this.props.mailboxFilter == 'sent ? '!' : '';
bangOrNot( this.currentUser.canActOnBehalfOf( m.senderID ) );

As in, is there a way to avoid the extended if/else syntax and all the repetition by choosing whether or not to call the return line with a bang?

Not sure how better to word this (and, consequently, couldn't find previous answers, though I expect this has been answered before), but I'm interested in whether there's a way to turn code like this:

if ( this.props.mailboxFilter == 'sent' ) {
  return this.currentUser.canActOnBehalfOf( m.senderID );
} else {
  return !this.currentUser.canActOnBehalfOf( m.senderID );
}

To something like the below (not sure how better to express it):

var bangOrNot = this.props.mailboxFilter == 'sent ? '!' : '';
bangOrNot( this.currentUser.canActOnBehalfOf( m.senderID ) );

As in, is there a way to avoid the extended if/else syntax and all the repetition by choosing whether or not to call the return line with a bang?

Share Improve this question edited Dec 16, 2015 at 1:31 Felix Kling 817k181 gold badges1.1k silver badges1.2k bronze badges asked Dec 15, 2015 at 22:10 SashaSasha 6,47613 gold badges60 silver badges107 bronze badges 1
  • 1 The solution might be elsewhere in your logic. Do you really want to send the inverse of the permissions? That seems weird. – isherwood Commented Dec 15, 2015 at 22:15
Add a ment  | 

5 Answers 5

Reset to default 9

You could simplify it in other ways:

var response = this.currentUser.canActOnBehalfOf(m.senderID);
return this.props.mailboxFilter == 'sent' ? response : !response;

In general, you would want to avoid this method if your function changes any state. However, since in this case you are calling it regardless, there's no harm in caching its value first.

If this.currentUser.canActOnBehalfOf( m.senderID ) allways returns a boolean value, you could do XOR.

return (this.props.mailboxFilter != 'sent') ^ this.currentUser.canActOnBehalfOf(m.senderID);

XOR a boolean value with true makes a NOT. And XOR with false makes a buffer which maintains the same value.

Note that I changed the == to !=.

And, just make sure you put ments on this part of the code if you are going to use it actually. It is not that easy to read this code after a while.

Update

As Bergi suggested, it is better to use Boolean XOR != to return a boolean value.

return (this.props.mailboxFilter != 'sent') != this.currentUser.canActOnBehalfOf(m.senderID);

And of course if it can be used with boolean XNOR as well, just replace both !=s with == in the code above.

You want to use the boolean XNOR operator here - also known as equivalence:

return this.currentUser.canActOnBehalfOf(m.senderID) == (this.props.mailboxFilter == 'sent');

There are many good ments regarding why or why NOT to do such thing. I'm assuming the OP has good reason to want to apply the bang (!) in front of the result of the function call. My answer is more of a technical solution to the OP's question.

function bang(r) {
    return !r;
}
function notBang(r) {
    return r;
}
function canActOnBehalf() {
    return true;
}
var filter = 'notsent';
var f = (filter == 'sent' ? bang : notBang);
f(canActOnBehalf('whatever'));

Assuming you're using javascript, you can use the eval method to evaluate a string of javascript code. You can conditionally apply the bang to the front of the string and then return the result of eval.

发布评论

评论列表(0)

  1. 暂无评论