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
.