通过自定义构造函数所new出来的对象模拟实现max()/min()
function MyMath() {
//添加方法
// 在当前构造函数中的this指向当前new出来的实例对象
this.getMax = function () {
//接收所有的实参 arguments 实参的值(伪数组,可以用length属性, 本质是对象)
console.log(arguments);
var max = arguments[0];
for(var i = 0; i <arguments.length; i++){
if(max < arguments[i]){
max = arguments[i];
}
}
return max;
};
this.getMin = function () {
//假设最小值
var min = arguments[0];
for(var i = 0; i <arguments.length; i++){
if(min > arguments[i]){
min = arguments[i];
}
}
return min;
}
}
var myMath = new MyMath();
console.log(myMath.getMax(10, 20, 1, 5));
console.log(myMath.getMin(10, 20, 1, 5));
console.log(Math.max(10, 20, 1, 5));
console.log(Math.min(10, 20, 1, 5));