/**
* 比较版本号的大小,如果curV 大于 reqV,则返回true,否则返回false
* @param {String} curV
* @param {String} reqV
*/
function Version(curV, reqV) {
var arr1 = curV.toString().split('.');
var arr2 = reqV.toString().split('.');
//将两个版本号拆成数字
var minL = Math.min(arr1.length, arr2.length);
var pos = 0; //当前比较位
var diff = 0; //当前为位比较是否相等
var flag = false;
//逐个比较如果当前位相等则继续比较下一位
while(pos < minL) {
diff = parseInt(arr1[pos]) - parseInt(arr2[pos]);
if(diff == 0) {
pos++;
continue;
} else if(diff > 0) {
flag = true;
break;
} else {
flag = false;
break;
}
}
return flag;
}
例如:let test_v = Version(‘5.1.3’,‘5.0.1’);
console.log(test_v ); //true