趣味示例

写 if 时不带 else,你的代码会更好

funtion performOperation (number) {
    if (number > 5) {
        // do something
    } else {
        // do somethingelse 
    }      
}

// 不带else
funtion performOperation (number) {
    if (number > 5) {
        // do something
        return
    }
    // do somethingelse      
}
funtion determineGender (type) {
    let gender = ''
    if (type === 0) {
        gender = 'male'
    } else if (type === 1) {
        gender = 'woman'
    } else {
        gender = 'unknown'
    }
    return gender
}

// 不带else
funtion determineGender (type) {
    if (type === 0) { return 'male' }
    if (type === 1) { return 'woman' }
    return'unknown'
}

最后更新于