The covering problem of js prototype chain function

the same prototype chain function, pass in different parameters and get different results, but the latter function overrides the former function, and the two functions output the same result. This is an example of sorting. The priority is age- > sex- > id
var arr = [

.
        {"name":"aa","id":1000,"sex":0,"age":28},
        {"name":"bb","id":1001,"sex":1,"age":18},
        {"name":"cc","id":1002,"sex":0,"age":18},
        {"name":"cc","id":1003,"sex":1,"age":18}
        
      ];
      
      //1.

function upsort (first,second,third) {

return function(a,b){
    var v1 = a[first];
    var v2 = b[first];
    var res = v1 - v2;
    if(res == 0){
        upsort(first,second,third);
        var v3 = a[second];
        var v4 = b[second];
        var res2 = v3 - v4;
        return res2;
        if(res2 == 0){
            upsort(first,second,third);
            var v5 = a[third];
            var v6 = b[third];
            var res3 = v5 - v6;
            return res3;
        }
    }
    return res;
}

}

    //2.

function downsort (first,second,third) {

return function(a,b){
    var v1 = a[first];
    var v2 = b[first];
    var res = v2 - v1;
    if(res == 0){
        downsort(first,second,third);
        var v3 = a[second];
        var v4 = b[second];
        var res2 = v4 - v3;
        return res2;
        if(res2 == 0){
            downsort(first,second,third);
            var v5 = a[third];
            var v6 = b[third];
            var res3 = v6 - v5;
            return res3;
        }
    }
    return res;
}

}

function sortPlugin () {}
sortPlugin.prototype.str = 1; / 1 is ascending order, 2 is descending order, default is ascending order
sortPlugin.prototype.sortWay = function (str = this.str) {

if(str == 1){
    console.log(arr.sort(upsort("age","sex","id")));
}else if(str == 2){
    console.log(arr.sort(downsort("age","sex","id")));
}

}

var sp = new sortPlugin ();
sp.sortWay ();

sp2.sortWay (2);

it"s okay to run two functions alone. I don"t know what went wrong. The prototype chain hasn"t eaten through yet

.
Mar.07,2021

this is not a problem of function coverage, but the reason why the active object is still active. The most common example is that the asynchronously requested data is first printed to the console, and after a series of operations, the structure after the operation is printed to the console.

you can see the effect by changing the code here

if(str == 1){
    console.log(JSON.parse(JSON.stringify(arr.sort(upsort("age","sex","id")))));
}else if(str == 2){
    console.log(JSON.parse(JSON.stringify(arr.sort(downsort("age","sex","id")))));
}

in addition, there is a slight mistake in your two sorting methods: return res2; here you should determine whether it is 0 or not, and then return

.

sort optimization is posted here
`var arr = [

{ "name": "aa", "id": 1000, "sex": 0, "age": 28 },
{ "name": "bb", "id": 1001, "sex": 1, "age": 18 },
{ "name": "cc", "id": 1002, "sex": 0, "age": 18 },
{ "name": "cc", "id": 1003, "sex": 1, "age": 18 }

];

1. Ascending
function upsort () {

var sortColumns = arguments;
 
return function (a, b) {
    if (sortColumns.length == 0) {
        return a - b;
    }

    for (var i = 0; i < sortColumns.length; iPP) {
        var compareResult = a[sortColumns[i]] - b[sortColumns[i]];
        if (i == sortColumns.length - 1 || compareResult != 0) {
            return compareResult;
        }
    }
}

}

2. Descending
function downsort () {

var sortColumns = arguments;

return function (a, b) {
    if (sortColumns.length == 0) {
        return b - a;
    }

    for (var i = 0; i < sortColumns.length; iPP) {
        var compareResult = b[sortColumns[i]] - a[sortColumns[i]];
        if (i == sortColumns.length - 1 || compareResult != 0) {
            return compareResult;
        }
    }
}

}

function sortPlugin () {}
sortPlugin.prototype.str = 1; / 1 is ascending order, 2 is descending order, default is ascending order
sortPlugin.prototype.sortWay = function (str) {

if (str != undefined) {
    this.str = str;
}
 
if (this.str == 1) {
    console.log(JSON.stringify(arr.sort(upsort("age", "sex", "id")))); //JSONconsole
} else if (this.str == 2) {
    console.log(JSON.stringify(arr.sort(downsort("age", "sex", "id"))));
}

}

var sp = new sortPlugin ();
sp.sortWay (1);
sp.sortWay (2); `

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b36033-2bfc1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b36033-2bfc1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?