javascript中的链式三元运算符

时间:2015-05-22 16:06:49

标签: javascript

我想轻松格式化,以便任何人都能理解最终输出。否则我想使用if条件重新编码。任何人都可以帮助我理解以下代码并以更明显的理解方式对其进行格式化。

validate: function() {
    return this.getRequired() && 
   !this.getValue() ? 
        (this.showError("Required"), !1) 
        : this.getValue() ? 
        "email" != this.getType() || this.isValidEmail() ? 
            "email_username" != this.getType() || this.isValidEmail() || this.isValidUsername() ? 
                    "username" != this.getType() || this.isValidUsername() ?
                            "roomid" != this.getType() || this.isValidUsername() ? 
                                        "displayName" != this.getType() || this.isValidDisplayName() ? 
                                                    "phone" != this.getType() || this.isValidPhone() ? !0 
                                                    : (this.showError("Enter a valid phone number"), !1) 
                                        :(this.showError("Enter both first and last names"), !1)  
                            :(this.showError("Enter a valid meeting room ID"), !1) 
                    :(this.showError("Enter a valid username"), !1) 
            :(this.showError("Enter a valid email address or username"), !1) 
        :(this.showError("Enter a valid email address"), !1) 
    : !0
}

4 个答案:

答案 0 :(得分:1)

不确定这是否有用?

var items = ['email', 'email_username', 'etc'];
for(var i=0;i<items.length;i++){
    switch(items[i]){
        case 'email':
            if(items[i] != this.getType || items[i].isValidEmail())
            this.showError('Enter a valid email address'), !1) 
            break;
        case 'email_username':
            if(items[i] != this.getType || items[i].isValidEmail() || this.isValidUsername())
            this.showError('Enter a valid email address'), !1) 
            break;
    }
}

答案 1 :(得分:0)

这是代码的一部分,我只为电子邮件做过。你需要做其余的事情:

validate: function () {
    var type = this.getType();
    var isValid = true;
    if (this.getRequired() && !this.getValue()) {
        switch (type) {
            case "email":
                if (!this.isValidEmail()) {
                    isValid = false;
                    this.showError("Enter a valid email address")
                }
                break;
            case "email_username":
                break;
            case "username":
                break;
            case "roomid":
                break;
            case "displayName":
                break;
            case "phone":
                break;
        }
    }
    return isValid;

}

答案 2 :(得分:0)

这实际上并不可读。它的格式很糟糕。我会这样格式化:

#include <stdio.h>
#include <stdlib.h>

// convert the calculated greyscale to a character based on brightness
char method_of_conversion(int greyscale){
    if(greyscale >= 230){
        return ' ';
    }else if(greyscale >= 200 && greyscale < 230){
        return '.';
    }else if(greyscale >= 180 && greyscale < 200){
        return '\'';
    }else if(greyscale >= 160 && greyscale < 180){
        return ':';
    }else if(greyscale >= 130 && greyscale < 160){
        return 'o';
    }else if(greyscale >= 100 && greyscale < 130){
        return '&';
    }else if(greyscale >= 70 && greyscale < 100){
        return '8';
    }else if(greyscale >= 50 && greyscale < 70){
        return '#';
    }else if(greyscale < 50){
        return '@';
    }
}

int main(){
    char ppmFile[100];
    char outputFile[100];

    int n;

    scanf("%s", &ppmFile); //read the name of input file
    scanf("%s", &outputFile); //read the name of output file 
    // the size of a window of pixels you have to convert to ascii art character
    scanf("%d", &n); 

    FILE *input = fopen(ppmFile, "rb");
    FILE *output = fopen(outputFile, "w"); 


    char header[5]; //header = P6
    fscanf(input, "%s\n", header);
    int width, height, maxPixel; // max pixel is always 255
    // read the header from the ppm file
    fscanf(input, "%d %d %d\n", &width, &height, &maxPixel);

    // allocate place for array[width][length][3]
    int ***array;
    array = malloc(width*sizeof(int **));
    array[0] = malloc(height*sizeof(int *));
    array[0][0] = malloc(3*sizeof(int));

    int x, y;
    for (x = 0; x < width; x++){ 
        for(y=0; y < height; y++){
            array[x][y][0] = fgetc(input); //red
            array[x][y][1] = fgetc(input); //green
            array[x][y][2] = fgetc(input); //blue

            int greyscale;
            int i, j;
            // convert blocks of pixels to a character and write it into output file
            for(i = 0; i < width; i+=n){
                for(j=0; j < height; j+=n){
                    // greyscale = (red + green +blue)/3;
                    greyscale = (array[x][y][0] + array[x][y][1] +array[x][y][2])/(3*n*n);
                    char c = method_of_conversion(greyscale);
                    fprintf(output,"%c",c); // write the ASCII art directly in the output file
                }
            }   
        }fprintf(output,"\n"); // dont forget to go into a new line
    }

    free(array);
    fclose(input);
    fclose(output);

    return 0;
}

这看起来肯定很粗糙,但它可以节省大量单独的返回语句,按照&#34;如果是这样,那么返回它,否则如果这样,则返回此....&#34;等

另外,是的,return this.getRequired() && !this.getValue() ? (this.showError("Required"), false) : this.getValue() ? "email" != this.getType() || this.isValidEmail() ? "email_username" != this.getType() || this.isValidEmail() || this.isValidUsername() ? "username" != this.getType() || this.isValidUsername() ? "roomid" != this.getType() || this.isValidUsername() ? "displayName" != this.getType() || this.isValidDisplayName() ? "phone" != this.getType() || this.isValidPhone() ? true : (this.showError($L("Enter a valid phone number")), false) : (this.showError("Enter both first and last names"), false) : (this.showError("Enter a valid meeting room ID"), false) : (this.showError("Enter a valid username"), false) : (this.showError("Enter a valid email address or username"), false) : (this.showError("Enter a valid email address"), false) : true 语法完全是多余的。

编辑:交换机在这里工作的原因是因为逻辑是嵌套的。您最终会得到替换三元组的嵌套if语句,或者您必须重写逻辑。如果它正在工作......很棒。测试它,看看它是否可以重构。如果不是......我希望你有这个事情的商业规则。

答案 3 :(得分:0)

为什么不使用这样一些更易读的格式:

validate: function () {
    var ok = false;
    switch (true) {
        case (this.getRequired() && !this.getValue()):
            this.showError("Required");
            break;
        case ("email" === this.getType() && !this.isValidEmail()):
            this.showError("Enter a valid email address");
            break;//                                         
        case ("email_username" === this.getType() && (!this.isValidEmail() || !this.isValidUsername())):
            this.showError("Enter a valid email address or username");
            break;
        case ("username" === this.getType() && !this.isValidUsername()):
            this.showError("Enter a valid username");
            break;
        case ("roomid" === this.getType() && !this.isValidUsername()):
            this.showError("Enter a valid meeting room ID");
            break;
        case ("displayName" === this.getType() && !this.isValidDisplayName()):
            this.showError("Enter both first and last names");
            break;
        case ("phone" !== this.getType() && !this.isValidPhone()):
            this.showError($L("Enter a valid phone number"));
            break;
        default: ok = true;
    }
    return ok;
}