小编mal*_*dro的帖子

使用if语句声明const的值

我有一个const numberOfComments是数组的大小,const riskByComments如果numberOfComments要大于2,我想拥有另一个值为5的值,否则应为0。

我知道我可以使用轻松地做到这一点let,但是我知道最好只使用const

    export const calculateRisk = (ticket) => dispatch => {
      const numberOfComments = ticket.comments.length
      let riskByComments//if i use const i need to declare its value
         //right away, i cannot use an if statement to do it later
      if(ticket.comments.length>2){
        riskByComments=5//if i use const inside the if statement
      }else{
         riskByComments=0//i cannot use it in the rest of my function
       }

Run Code Online (Sandbox Code Playgroud)

javascript const

3
推荐指数
1
解决办法
65
查看次数

我创建的班级未导出对象

我需要在文件ShoppingCart.js中创建类ShoppingCart并将其导出到测试文件,但出现错误,提示我的类不是构造函数

我知道问题不在导入导出中,因为在创建js文件之前,我得到了找不到模块的错误。我还尝试在文件中创建该类的新实例,并且该方法有效

file ShoppingCart.js
class ShoppingCart{
    constructor(name){
        this.name=name
    }
}

module.exports = { ShoppingCart}
Run Code Online (Sandbox Code Playgroud)

我的测试文件的代码是

 const ShoppingCart = require("./ShoppingCart")
 new ShoppingCart()
Run Code Online (Sandbox Code Playgroud)

当我运行测试文件时,我得到

TypeError: ShoppingCart is not a constructor
Run Code Online (Sandbox Code Playgroud)

javascript class object

0
推荐指数
1
解决办法
42
查看次数

标签 统计

javascript ×2

class ×1

const ×1

object ×1