简体   繁体   English

JavaScript简写if +简写赋值

[英]Javascript shorthand if + shorthand assignment

I'm wondering if there's a way to merge shorthand if/else with shorthand += 我想知道是否有一种方法可以将简写if / else与简写+ =合并

Something like this: 像这样:

var value;
$.each(data.events, function(index, element) {
    var otherValue = element.value;
    value = value ? value + otherValue : '';
}

Something to prevent 预防的东西

value += otherValue

From adding 'undefined' to the beginning when value is undefined. 从在值未定义时开始添加“未定义”开始。

The long version would be: 长版本将是:

var value;
$.each(data.events, function(index, element) {
    var otherValue = element.value;
    if(value){
        value = value + otherValue;
    }

}

I hope the question is not (too) confusing :) 我希望这个问题不要太混乱:)

Just like this: 像这样:

value = value && value + otherValue || value

another possible way would be: 另一种可能的方式是:

value && (value += otherValue)

it's just like if value is truthy evaluate the next term (value += otherValue) 就像value是真实的一样,请评估下一个条件(value += otherValue)

Though I would not go for these paths, I think one of the things we need to consider in coding is not just about how short our code is, but also readability. 尽管我不会走这些路,但我认为我们在编码中需要考虑的一件事不仅是代码的短短,还在于可读性。

I would still prefer 我还是喜欢

if(value)
    value += otherValue;

because it is easier to read and see that you had a condition there 因为它更容易阅读和查看您那里有病

Edit: 编辑:

Geeze what I posted is almost the exact opposite of what you had in your bottom example. 我发布的Geeze几乎与您在下面示例中的内容完全相反。 I would have deleted my question here but its accepted =/ 我会在这里删除我的问题,但已接受= /

You can use the AND && operator: 您可以使用AND &&运算符:

console.log('foo' && 'hello'); // prints hello
console.log(null && 'hello'); // prints null
console.log(undefined && null); // prints undefined
console.log('foo' && null && 'bar'); // prints null


var value;
$.each(data.events, function(index, element) {
    // if value is undefined, null, or empty then it stays the same.
    // otherwise add append the element value
    value = (value && value + element.value);
}

although this isnt much more readable than your original 尽管这并不比您的原始书更具可读性

var value;
$.each(data.events, function(index, element) {
    // if value is undefined, null, or empty then it stays the same.
    // otherwise add append the element value
    if(value) value += otherValue;
}

I left my original answer below, I had read your question and saw your first code snippet and answered to that. 我在下面留下了原始答案,我已经阅读了您的问题,并看到了您的第一个代码段并对此进行了回答。 However your 2nd code snippet does something different, im not really sure what answer is right now... 但是您的第二个代码片段做了一些不同的事情,我不确定现在的答案是什么...


You could || 您可以|| operator which will return the first undefined value it saw as the expression is true (eg: !!val === true ) or the last value in the operator sequence (assuming your using all OR statements || ) 运算符,它将返回表达式为true时看到的第一个未定义值(例如: !!val === true )或运算符序列中的最后一个值(假设您使用所有OR语句||

console.log(undefined || 'hello'); // prints hello
console.log('' || 'hello'); // prints hello
console.log(undefined || null); // prints null
console.log(undefined || '' || null); // prints null

So in your case taking your working but longer JavaScript code we can simplify it to the following 因此,在您的情况下,使用您的工作时间更长的JavaScript代码,我们可以将其简化为以下内容

var value;
$.each(data.events, function(index, element) {
    value = (value && value+element.value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM