[英]Javascript Syntax: AngularJS Filter
I have found this excellent code for applying a filter in AngularJS here . 我发现这个优秀的代码在AngularJS应用过滤器在这里 。 It works as expected, although I'm a little unsure on what the shortening using !
它可以按预期工作,尽管我不确定使用哪种缩短! ?
? and : in this context would replace.
和:在这种情况下将替换。
From experience, it looks like the ? 从经验来看,它看起来像? and : are a ternary operators, although I'm not entirely certain what function the !
和:是三元运算符,尽管我不确定! has.
拥有。
From looking around stackoverflow, it seems like the ! 从stackoverflow的四周看,似乎是! has multiple functions depending on context, ie 3755606 .
具有取决于上下文的多个功能,即3755606 。
app.filter('myFilterName', function () { return function (value) { return (!value) ? '' : value.replace(/ /g, ''); }; });
The !
!
is the logical NOT operator : It coerces its argument to a boolean value, then inverts that value (eg, true becomes false and false becomes true). 是逻辑NOT运算符 :将其参数强制为布尔值,然后将该值取反(例如,true变为false,false变为true)。
if (!value)
checks to see if value
has a falsey value. if (!value)
检查value
是否具有假值。 What's a "falsey" value? 什么是“假”值? Any value that becomes false when coerced to a boolean.
强制为布尔值时变为false的任何值。 That's
null
, undefined
, ""
, NaN
, 0
, and of course, false
. 这是
null
, undefined
, ""
, NaN
, 0
,当然是false
。
The test ? one_thing : another_thing
test ? one_thing : another_thing
test ? one_thing : another_thing
is the conditional operator . test ? one_thing : another_thing
是条件运算符 。 If the value that is being tested is truthy, the expression after the ?
如果要测试的值是真实的,则
?
之后的表达式 is evaluated and the conditional expression takes that result; 被评估并且条件表达式得到那个结果; if not, the expression after the
:
is evaluated and the conditional expression takes that result. 如果不是,则对
:
之后的表达式求值,条件表达式采用该结果。 (You'll sometimes hear the conditional operator called the "ternary" operator. It's true that it's a ternary operator [an operator that accepts three operands], and it's the only ternary operator currently in JavaScript, but its proper name is the 'conditional operator'. In theory, someday other ternary operators could be added to the language...) (有时您会听到称为“三元”运算符的条件运算符。的确,它是一个三元运算符(一个接受三个操作数的运算符),并且它是JavaScript中当前唯一的三元运算符,但其专有名称是“条件运算符从理论上讲,有一天可以将其他三元运算符添加到该语言中...)
So the net result of: 因此,最终结果是:
return (!value) ? '' : value.replace(/ /g, '');
is: 是:
Coerce value
to boolean: Falsey values become false
, all other values become true
. 强制
value
布尔:Falsey值变为false
,所有其他值成为true
。
Invert that boolean. 反转该布尔值。
If the inverted boolean is true
, return ''
; 如果倒数布尔为
true
,则返回''
; otherwise, evaluate value.replace(/ /g, '')
and return the result. 否则,评估
value.replace(/ /g, '')
并返回结果。
My guess is that value
is always a string. 我的猜测是
value
始终是一个字符串。 If so, then the short version is: If value
is ""
, return ""
; 如果是这样,则简短版本为:如果
value
为""
,则返回""
; otherwise, return the contents of value
with all spaces removed. 否则,返回
value
的内容并删除所有空格。
Side note: the ()
around !value
are completely unnecessary. 旁注:
!value
周围的()
完全不必要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.