Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript简化条件_Javascript_Return_Conditional Statements - Fatal编程技术网

Javascript简化条件

Javascript简化条件,javascript,return,conditional-statements,Javascript,Return,Conditional Statements,我想寻求一个帮助来简化这种情况,因为存在重复的问题 if (this.get('fileUrl')) { if (this.get('fileContainer.asset_kind') === 'UnsplashAsset') { return Asset.create({ url: this.get('fileUrl'), mime_type: this.get('fileContainer.mime_type'),

我想寻求一个帮助来简化这种情况,因为存在重复的问题

if (this.get('fileUrl')) {
      if (this.get('fileContainer.asset_kind') === 'UnsplashAsset') {
        return Asset.create({
          url: this.get('fileUrl'),
          mime_type: this.get('fileContainer.mime_type'),
          isUnsplashObject: true
        });
      } else {
        return Asset.create({
          url: this.get('fileUrl'),
          mime_type: this.get('fileContainer.mime_type'),
          isUnsplashObject: false
        });
      }
    }

您可以像这样简单地更新
isunplashobject
,并返回单个
Asset.create({…})
像:

if (this.get('fileUrl')) {
  return Asset.create({
    url: this.get('fileUrl'),
    mime_type: this.get('fileContainer.mime_type'),
    isUnsplashObject: (this.get('fileContainer.asset_kind') === 'UnsplashAsset')
  });
}

这与执行以下操作类似:

if (this.get('fileContainer.asset_kind') === 'UnsplashAsset') {
  isUnsplashObject = true
} else {
  isUnsplashObject = false
}
或者,使用三元运算符,如:

isUnsplashObject = (this.get('fileContainer.asset_kind') === 'UnsplashAsset') ? true : false;
或者,正如
==
相等性检查已经返回布尔值一样,我们只需返回该值:

isUnsplashObject = (this.get('fileContainer.asset_kind') === 'UnsplashAsset')

唯一改变的是UnsplashObject,所以把条件放在那里

if(this.get('fileUrl')){
返回资产。创建({
url:this.get('fileUrl'),
mime类型:this.get('fileContainer.mime类型'),
isUnplashObject:this.get('fileContainer.asset\u kind')==='UnplashAsset'
});
}