有人可以解释一下这段 JavaScript 代码吗?
我是 JavaScript 开发的新手,如果有人能帮助我解释这段代码,我将不胜感激。
m = function () {
let t = e.getCookie("cli-analytics"),
a = e.getCookie("cli-advertisement"),
r = e.getCookie("CLI");
return !(!t && !a) && { analytics: "yes" === t, ads: "yes" === a, visitorHasChosen: !!r };
}
我可以理解对变量 m 的函数赋值和 cookie 读取,但我无法理解 return 实际执行的操作(返回一个对象并与复杂的逻辑 NOT 进行 AND 运算?在对象内部分配了一个属性双重非?)。 顺便问一下,这是很好的编程实践/写作吗?
感谢大家
I am fairly beginner to JavaScript development and I would be obliged if someone could help explain this code to me.
m = function () {
let t = e.getCookie("cli-analytics"),
a = e.getCookie("cli-advertisement"),
r = e.getCookie("CLI");
return !(!t && !a) && { analytics: "yes" === t, ads: "yes" === a, visitorHasChosen: !!r };
}
I can understand the function assignment to the variable m and the cookie reads, but I have trouble understanding what the return actually does (returning an object ANDed with a complex logical NOT? Inside the object a property is assigned a double NOT?).
By the way is this good programming practice/writing?
Thanks to all
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如已经提到的,代码首先被缩小,然后再次被漂亮化。
因此,人们只需从
getCookie
方法的“speaking”字符串值就可以理解代码的意图。从这里开始,根据t
、a
和r
的缩短变量名称,人们可以想出自己有意义的变量名称和替换前者由后者。然后,正如已经指出的,人们确实将名称替换和德摩根定律应用于以下表达式...
!(!analyticsValue && !advertisementValue)
!0 === true
,!'' == = true
,!{} === false
。(!!analyticsValue || !!advertisementValue)
!!0 === false
,!!'' === 假
,!!{} === 真
。(analyticsValue ||advertisementValue)
(0 || 1) === 1, <代码>(2 || '') === 2
...但是(2 && '') === ''
,(2 && 3) === 3
,('' && 3) === ''
....
2)
读取 ... “如果任一值存在(为true
)”... 对于 < code>3) ... “如果其中一个值为真”。
由于对于以下恢复的示例代码,仍然不清楚
e
来自何处以及它的含义/代表什么,因此该示例假设一个自定义 cookiestorage
被视为函数的唯一参数...可以将上面恢复的函数重写为更具表现力/更直接的东西,但由于返回值的计算过程并不完全遵循原始函数的计算过程,因此返回值为falsy与中间值相比可能有所不同原来的实现。
As already mentioned, the code was first minified and then prettyfied again.
Thus one can comprehend the code's intension just from the "speaking" string values of the
getCookie
method. From there and from following the shortened variable names oft
,a
andr
one can come up with own meaningful variable names and a substitution of the former by the latter.Then, as already pointed to, one does apply name substitution and DeMorgan's law to the following expression ...
!(!analyticsValue && !advertisementValue)
!0 === true
,!'' === true
,!{} === false
.(!!analyticsValue || !!advertisementValue)
!!0 === false
,!!'' === false
,!!{} === true
.(analyticsValue || advertisementValue)
(0 || 1) === 1
,(2 || '') === 2
... but(2 && '') === ''
,(2 && 3) === 3
,('' && 3) === ''
.... which for
2)
reads ... "if either of the values exists (istrue
)"... and for
3)
... "if either of the values is truthy".Since for the following restored example code it still is not clear where
e
does come from and what it means/represents, the example assumes a custom cookiestorage
which is treated as the function's sole parameter ...One could rewrite the above restored function to something more expressive / straightforward but of cause the computation process of the return value does not follow exactly the original one's, thus the return value for falsy intermediate values might differ in comparison with the original implementation.
如果满足此条件
!(!t && !a)
,则返回{ Analytics: "yes" === t, ads: "yes" === a, VisitorHasChosen :!!r}
If this condition
!(!t && !a)
is satisfied, return{ analytics: "yes" === t, ads: "yes" === a, visitorHasChosen: !!r }
条件:
如果
t
或a
之一具有假值或两者都有假值,则返回该对象
对象:
keys 的值是 boolean 类型,因为比较 (===) 或
!!
因此它会检查
t
的值是否等于“yes”,然后analytics
的值必须为true
,否则为false
,它也在ads
中发生!!r
invisitorHasChosen
is Eqaul toBoolean(r)
condition:
if one of
t
ora
has a falsy value or both of them has a falsy value, return thatobject
object:
the value of keys is a boolean type because comparing (===) or
!!
so it checks if the value of
t
equal 'yes' then the value ofanalytics
must betrue
otherwisefalse
, also it's happening inads
!!r
invisitorHasChosen
is Eqaul toBoolean(r)