使用 CoffeeScript 语法的 JavaScript - if 条件 : optimize if statement structure

标签 javascript function if-statement coffeescript

我有一个函数,正在测试 4 个变量,我想优化 if 语句测试的结构,有人可以帮忙吗? :

$scope.filterActivated = ->
    if $scope.postParams.options.scopes.report.from || $scope.postParams.options.scopes.report.to || $scope.postParams.options.template_id || $scope.displayOptions.query.length > 0
      return true
    else
      return false

最佳答案

您可以删除true/false并像这样优化它:

$scope.filterActivated = ->
  options = $scope.postParams.options
  options.scopes.report.from or options.scopes.report.to or options.template_id or $scope.displayOptions.query.length > 0

编辑:JS给你:

$scope.filterActivated = () => {
  let options = $scope.postParams.options;
  return options.scopes.report.from || options.scopes.report.to || options.template_id || $scope.displayOptions.query.length > 0;
};

关于使用 CoffeeScript 语法的 JavaScript - if 条件 : optimize if statement structure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52651463/

相关文章:

c - 如何使用 C 中的函数生成随机数数组

c++ - if 条件为一个数的倍数

javascript - 从字符串数组映射索引

javascript - 什么时候使用reduce和reduceRight?

javascript - 仅在 IE 中的 JS 数组推送错误

javascript - 更新后缺少 Meteor.js 图标

r - mutate `:=` 和 mutate `=` 之间的 tidyeval 差异

c - 十进制到二进制函数中的段错误

java - 空检查后抛出空错误?

ios - 为什么我无法在 Swift 中验证登录按钮中的文本字段