【问题标题】:What does this arrow function JavaScript code mean?这个箭头函数 JavaScript 代码是什么意思?
【发布时间】:2016-12-09 08:56:21
【问题描述】:

我正在尝试了解 HomeAssistant 前端源代码。我发现了我不太了解的函数定义。我不明白这种语法(model.entity 是一个字符串)...

export function createHasDataGetter(model) {
  return [
    ['restApiCache', model.entity],
    entityMap => !!entityMap,
  ];
}

看起来像:

return [[string, string], bool]?

这个函数的确切 teturn 类型是什么?这只是布尔吗?如果是,是不是表示 entityMap 是字符串数组?

【问题讨论】:

  • 如果你已经知道它是一个箭头函数,你还有什么困惑?好像您已经知道它是一个函数而不是 bool
  • entityMap => !!entityMap 等价于function ( entityMap ) { return !! entityMap; }
  • 好的,那么 createHasDataGetter 究竟返回了什么?是布尔吗?由箭头函数返回?为什么 return 语句有 2 个参数(字符串数组和函数返回 bool)

标签: javascript


【解决方案1】:

"Truthy" on MDN:

在 JavaScript 中,真值是在布尔上下文中计算时转换为 true 的值。所有值都是真实的,除非它们被定义为虚假(即,除了false0""nullundefinedNaN)。

entityMap => !!entityMapentityMap 映射到规范布尔值truefalse。另见What is "!!" in C?

如果entityMap 具有真值,则!entityMapfalse!!entityMaptrue

如果entityMap 有一个假值,那么!entityMaptrue!!entityMapfalse

【讨论】:

    猜你喜欢
    • 2021-05-10
    • 1970-01-01
    • 2014-03-16
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多