有人可以解释一下这段 JavaScript 代码吗?

发布于 2025-01-11 15:56:40 字数 467 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静待花开 2025-01-18 15:56:41

正如已经提到的,代码首先被缩小,然后再次被漂亮化。

因此,人们只需从 getCookie 方法的“speaking”字符串值就可以理解代码的意图。从这里开始,根据 tar 的缩短变量名称,人们可以想出自己有意义的变量名称和替换前者由后者。

然后,正如已经指出的,人们确实将名称替换和德摩根定律应用于以下表达式...

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

    • ... 求反将一个值转换为 true 布尔值,与该值本身的 true/falsy 表示相反 ... 例如 !0 === true, !'' == = true, !{} === false
  2. (!!analyticsValue || !!advertisementValue)

    • ...双重否定将一个值转换为该值自己的 true/falsy 表示的 true 布尔值...例如 !!0 === false, !!'' === 假!!{} === 真
  3. (analyticsValue ||advertisementValue)

    • ...两个值上的布尔运算符通过两个值的真值/假值表示来验证表达式,但返回真实值...例如 (0 || 1) === 1, <代码>(2 || '') === 2 ...但是 (2 && '') === '', (2 && 3) === 3, ('' && 3) === ''.

... 2) 读取 ... “如果任一值存在(为 true)”

... 对于 < code>3) ... “如果其中一个值为真”

由于对于以下恢复的示例代码,仍然不清楚 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;
}

可以将上面恢复的函数重写为更具表现力/更直接的东西,但由于返回值的计算过程并不完全遵循原始函数的计算过程,因此返回值为falsy与中间值相比可能有所不同原来的实现。

/*
 * @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;
}

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 of t, a and r 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 ...

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

    • ... negation casts a value into a true boolean value, opposing the value's own truthy/falsy representation ... e.g. !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 ... e.g. !!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 ... e.g. (0 || 1) === 1, (2 || '') === 2 ... but (2 && '') === '', (2 && 3) === 3, ('' && 3) === ''.

... which for 2) reads ... "if either of the values exists (is true)"

... 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 cookie storage which is treated as the function's sole parameter ...

/*
 * @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.

/*
 * @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;
}
眼眸里的那抹悲凉 2025-01-18 15:56:41

如果满足此条件 !(!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 }

醉生梦死 2025-01-18 15:56:41

条件:

如果 t a 之一具有假值或两者都有假值,则返回该对象

对象:

keys 的值是 boolean 类型,因为比较 (===) 或 !!
因此它会检查 t 的值是否等于“yes”,然后 analytics 的值必须为 true,否则为 false ,它也在 ads 中发生

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

condition:

if one of t or a has a falsy value or both of them has a falsy value, return that object

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 of analytics must be true otherwise false, also it's happening in ads

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文