查看Nubmer.prototype实例新增的方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>查看一下Nubmer.prototype实例,我们会发现多了几个全局方法add++sun+mul+div</title> </head> <body> <script type="text/javascript"> // 加法函数 function accAdd(arg1, arg2) { var r1, r2, m; try { r1 = arg1.toString().split(".")[1].length console.log(arg1.toString().split(".")[0]); console.log(arg1.toString().split(".")[1]); } catch (e) { r1 = 0 } try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 } m = Math.pow(10, Math.max(r1, r2)) return (arg1 * m + arg2 * m) / m } //给Number类型增加一个add方法,调用起来更加方便。 Number.prototype.add = function (arg) { return accAdd(arg, this); } console.log(accAdd(0.11, 0.2)); console.log(0.1.add(0.2)); // 减法函数 function accSub(arg1, arg2) { var r1, r2, m, n; try { r1 = arg1.toString().split(".")[1].length; } catch (e) { r1 = 0; } try { r2 = arg2.toString().split(".")[1].length; } catch (e) { r2 = 0; } m = Math.pow(10, Math.max(r1, r2)); n = (r1 >= r2) ? r1 : r2; return ((arg1 * m - arg2 * m) / m).toFixed(n); } Number.prototype.sub = function (arg) { return accSub(arg, this); } console.log(accSub(0.2, 0.1)); console.log(0.1.sub(0.2)); // 乘法函数 function accMul(arg1, arg2) { var m = 0, s1 = arg1.toString(), s2 = arg2.toString(); try { m += s1.split(".")[1].length; } catch (e) {} try { m += s2.split(".")[1].length; } catch (e) {} console.log(s1.replace(".", "")); console.log(s2.replace(".", "")); return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m); } Number.prototype.mul = function (arg) { return accMul(arg, this); } console.log(accMul(0.05, 0.05)); console.log(0.1.mul(0.2)); // 除法函数 function accDiv(arg1, arg2) { var t1 = 0, t2 = 0; var r1, r2; try { t1 = arg1.toString().split(".")[1].length; } catch (e) {} try { t2 = arg2.toString().split(".")[1].length; } catch (e) {} // with 语句的原本用意是为逐级的对象访问提供命名空间式的速写方式. 也就是在指定的代码区域, 直接通过节点名称调用对象。 // with 通常被当做重复引用同一个对象中的多个属性的快捷方式,可以不需要重复引用对象本身。 // https://blog.csdn.net/zwkkkk1/article/details/79725934 // with(Math){ // r1 = Number(arg1.toString().replace(".", "")); // r2 = Number(arg2.toString().replace(".", "")); // return (r1/r2)*pow(10, t2-t1); // } r1 = Number(arg1.toString().replace(".", "")); r2 = Number(arg2.toString().replace(".", "")); return (r1 / r2) * Math.pow(10, t2 - t1); } Number.prototype.div = function (arg) { return accDiv(arg, this); } console.log(accDiv(0.05, 0.05)); console.log(0.1.div(0.2)); </script> </body> </html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>查看一下Nubmer.prototype实例,我们会发现多了几个全局方法add++sun+mul+div</title>
</head>
<body>
<script type="text/javascript">
// 加法函数
function accAdd(arg1, arg2) {
var r1, r2, m;
try {
r1 = arg1.toString().split(".")[1].length
console.log(arg1.toString().split(".")[0]);
console.log(arg1.toString().split(".")[1]);
} catch (e) {
r1 = 0
}
try {
r2 = arg2.toString().split(".")[1].length
} catch (e) {
r2 = 0
}
m = Math.pow(10, Math.max(r1, r2))
return (arg1 * m + arg2 * m) / m
}
//给Number类型增加一个add方法,调用起来更加方便。
Number.prototype.add = function (arg) {
return accAdd(arg, this);
}
console.log(accAdd(0.11, 0.2));
console.log(0.1.add(0.2));
// 减法函数
function accSub(arg1, arg2) {
var r1, r2, m, n;
try {
r1 = arg1.toString().split(".")[1].length;
} catch (e) {
r1 = 0;
}
try {
r2 = arg2.toString().split(".")[1].length;
} catch (e) {
r2 = 0;
}
m = Math.pow(10, Math.max(r1, r2));
n = (r1 >= r2) ? r1 : r2;
return ((arg1 * m - arg2 * m) / m).toFixed(n);
}
Number.prototype.sub = function (arg) {
return accSub(arg, this);
}
console.log(accSub(0.2, 0.1));
console.log(0.1.sub(0.2));
// 乘法函数
function accMul(arg1, arg2) {
var m = 0,
s1 = arg1.toString(),
s2 = arg2.toString();
try {
m += s1.split(".")[1].length;
} catch (e) {}
try {
m += s2.split(".")[1].length;
} catch (e) {}
console.log(s1.replace(".", ""));
console.log(s2.replace(".", ""));
return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}
Number.prototype.mul = function (arg) {
return accMul(arg, this);
}
console.log(accMul(0.05, 0.05));
console.log(0.1.mul(0.2));
// 除法函数
function accDiv(arg1, arg2) {
var t1 = 0,
t2 = 0;
var r1, r2;
try {
t1 = arg1.toString().split(".")[1].length;
} catch (e) {}
try {
t2 = arg2.toString().split(".")[1].length;
} catch (e) {}
// with 语句的原本用意是为逐级的对象访问提供命名空间式的速写方式. 也就是在指定的代码区域, 直接通过节点名称调用对象。
// with 通常被当做重复引用同一个对象中的多个属性的快捷方式,可以不需要重复引用对象本身。
// https://blog.csdn.net/zwkkkk1/article/details/79725934
// with(Math){
// r1 = Number(arg1.toString().replace(".", ""));
// r2 = Number(arg2.toString().replace(".", ""));
// return (r1/r2)*pow(10, t2-t1);
// }
r1 = Number(arg1.toString().replace(".", ""));
r2 = Number(arg2.toString().replace(".", ""));
return (r1 / r2) * Math.pow(10, t2 - t1);
}
Number.prototype.div = function (arg) {
return accDiv(arg, this);
}
console.log(accDiv(0.05, 0.05));
console.log(0.1.div(0.2));
</script>
</body>
</html>