Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

去除多个if语句 #10

Open
homobulla opened this issue Sep 7, 2018 · 0 comments
Open

去除多个if语句 #10

homobulla opened this issue Sep 7, 2018 · 0 comments

Comments

@homobulla
Copy link
Owner

homobulla commented Sep 7, 2018

将所有的条件置于一个对象或数组中,变为属性的选取操作。

const foo = v => {
  if (v === 'name') {
    return 'bao'
  } else if (v === 'age') {
    return '18'
  } else if (v === 'height') {
    return '180'
  }
}
const cfg = {
  name: 'bao',
  age: '18',
  height: '180'
}
const foo = v => cfg[v]

// 数组配置法
if (value === 'hello' || value === 'world' || value === 'blabla') {
  // ...
}
// 配置数组形式
const rightValue = ['hello', 'world', 'blabla']
if (rightValue.includes[value]) {
  // ...
}

探索两种优雅的表单验证

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant