带有枚举值的Ecmascript函数参数

问题描述 投票:0回答:1

Ecmascript方法中是否可以具有参数的enum值?

例如这种情况

export const testFunc = (param1) => {

};

例如,param只能取"val1","val2","val3"的值

export const testFunc = (param = {"val1","val2","val3"}) =>{

};
javascript ecmascript-6 ecmascript-5
1个回答
1
投票

JS中没有枚举之类的东西,但是您可以检查参数是否为允许的值之一:

export const testFunc = (param) =>{
  if (!["val1","val2","val3"].includes(param)) {
    throw new Error('Invalid param passed');
  }
  // rest of function
};
© www.soinside.com 2019 - 2024. All rights reserved.