同一个原型链函数,传入不同参数,得出不同的结果,但后一个函数覆盖了前一个函数,两个函数输出相同的结果,此为排序的示例,排序优先级为age->sex->idvararr=[{"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.升序functionupsort(first,second,third){returnfunction(a,b){varv1=a[first];varv2=b[first];varres=v1-v2;if(res==0){upsort(first,second,third);varv3=a[second];varv4=b[second];varres2=v3-v4;returnres2;if(res2==0){upsort(first,second,third);varv5=a[third];varv6=b[third];varres3=v5-v6;returnres3;}}returnres;}}//2.降序functiondownsort(first,second,third){returnfunction(a,b){varv1=a[first];varv2=b[first];varres=v2-v1;if(res==0){downsort(first,second,third);varv3=a[second];varv4=b[second];varres2=v4-v3;returnres2;if(res2==0){downsort(first,second,third);varv5=a[third];varv6=b[third];varres3=v6-v5;returnres3;}}returnres;}}functionsortPlugin(){}sortPlugin.prototype.str=1;//1为升序,2为降序,默认为升序sortPlugin.prototype.sortWay=function(str=this.str){if(str==1){console.log(arr.sort(upsort("age","sex","id")));}elseif(str==2){console.log(arr.sort(downsort("age","sex","id")));}}varsp=newsortPlugin();sp.sortWay();sp2.sortWay(2);单独运行两个函数是没问题的,不知道哪里出了问题,原型链还没有吃透
相关分类