简体   繁体   English

有人可以解释这个 JavaScript 代码吗?

[英]Can someone explain this JavaScript code please?

I am fairly beginner to JavaScript development and I would be obliged if someone could help explain this code to me.我是 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 };
}

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?).我可以理解 function 对变量 m 的赋值和 cookie 读取,但我很难理解返回的实际作用(返回 object AND 与复杂的逻辑 NOT?在 object 中,属性被分配了双重 NOT?)。 By the way is this good programming practice/writing?顺便问一下,这是好的编程实践/写作吗?

Thanks to all谢谢大家

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.因此,仅从getCookie方法的“说话”字符串值就可以理解代码的意图。 From there and from following the shortened variable names of t , a and r one can come up with own meaningful variable names and a substitution of the former by the latter.从那里并遵循tar的缩短变量名,可以得出自己有意义的变量名,并将前者替换为后者。

Then, as already pointed to, one does apply name substitution and DeMorgan's law to the following expression...然后,正如已经指出的那样,确实将名称替换和DeMorgan 定律应用于以下表达式......

!(!t && !a)
  1. !(!analyticsValue && !advertisementValue)

    • ... negation casts a value into a true boolean value, opposing the value's own truthy/falsy representation... eg !0 === true , !'' === true , !{} === false . ...否定将一个值转换为真 boolean 值,与该值自身的真/假表示相反...例如!0 === true , !'' === true , !{} === false
  2. (!!analyticsValue || !!advertisementValue)

    • ... double negation casts a value into a true boolean value of the value's own truthy/falsy representation... eg !!0 === false , !!'' === false , !!{} === true . ...双重否定将一个值转换为该值自身的真/假表示的真 boolean 值...例如!!0 === false , !!'' === false , !!{} === true .
  3. (analyticsValue || advertisementValue)

    • ... a boolean operator on two values validates the expression by both of the values' truthy/falsy representations but returns the real value instead... eg (0 || 1) === 1 , (2 || '') === 2 ... but (2 && '') === '' , (2 && 3) === 3 , ('' && 3) === '' . ...两个值上的 boolean 运算符通过两个值的真实/虚假表示来验证表达式,但返回真实值...例如(0 || 1) === 1 , (2 || '') === 2 ... 但是(2 && '') === '' , (2 && 3) === 3 , ('' && 3) === ''

... which for 2) reads... "if either of the values exists (is true )" ...其中2)显示... “如果存在任何一个值(为true )”

... and for 3) ... "if either of the values is truthy" . ...对于3) ... “如果其中一个值是真实的”

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 cookie storage which is treated as the function's sole parameter...由于对于以下恢复的示例代码,仍然不清楚e的来源以及它的含义/代表,该示例假定一个自定义 cookie storage ,该存储被视为函数的唯一参数......

/*
 * @returns {Object|false}
 *  either returns an object which features cookie values
 *  or returns the `false` boolean value.
 */
function getCliCookieValue(storage) {

  const analyticsValue = storage.getCookie('cli-analytics');
  const advertisementValue = storage.getCookie('cli-advertisement');

  // instead of ...
  return (!!analyticsValue || !!advertisementValue) && {

    analytics: 'yes' === analyticsValue,
    ads: 'yes' === advertisementValue,
    visitorHasChosen: !!storage.getCookie('CLI'),
  };

  // // ... one might consider ...
  // /*
  //  * @returns {Object|null}
  //  *  either returns an object which features cookie values
  //  *  or returns the `null` value.
  //  */
  // return (!!analyticsValue || !!advertisementValue) ? {
  // 
  //   analytics: 'yes' === analyticsValue,
  //   ads: 'yes' === advertisementValue,
  //   visitorHasChosen: !!storage.getCookie('CLI'),
  // 
  // } : null;
}

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.可以将上面恢复的 function 重写为更具表现力/更直接的东西,但由于返回值的计算过程并不完全遵循原始的,因此与原始实现相比,虚假中间值的返回值可能会有所不同。

/*
 * @returns {Object|false}
 *  either returns an object which features cookie values
 *  or returns the `false` boolean value.
 */
function getCliCookieValue(storage) {

  const isAnalytics = ('yes' === storage.getCookie('cli-analytics'));
  const isAdvertisement = ('yes' === storage.getCookie('cli-advertisement'));

  // instead of ...
  return (isAnalytics || isAdvertisement) && {

    analytics: isAnalytics,
    ads: isAdvertisement,
    visitorHasChosen: !!storage.getCookie('CLI'),
  };

  // // ... one might consider ...
  // /*
  //  * @returns {Object|null}
  //  *  either returns an object which features cookie values
  //  *  or returns the `null` value.
  //  */
  // return (isAnalytics || isAdvertisement) ? {
  // 
  //   analytics: isAnalytics,
  //   ads: isAdvertisement,
  //   visitorHasChosen: !!storage.getCookie('CLI'),
  // 
  // } : null;
}

If this condition !(!t && !a) is satisfied, return { analytics: "yes" === t, ads: "yes" === a, visitorHasChosen: !!r }如果这个条件!(!t && !a)满足,返回{ analytics: "yes" === t, ads: "yes" === a, visitorHasChosen: !!r }

condition:健康)状况:

if one of t or a has a falsy value or both of them has a falsy value, return that object如果ta之一具有假值或两者均具有假值,则返回object

object: object:

the value of keys is a boolean type because comparing (===) or !!键的值是 boolean 类型,因为比较 (===) 或!! so it checks if the value of t equal 'yes' then the value of analytics must be true otherwise false , also it's happening in ads所以它检查t的值是否等于 'yes' 那么analytics的值必须为true否则为false ,它也在ads中发生

!!r in visitorHasChosen is Eqaul to Boolean(r) !!r in visitorHasChosen等于Boolean(r)

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

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