内置对象
js三种对象:
- 内置对象 系统自带
- 自定义对象
- 浏览器对象 BOm
绝对值 [1 == 1][-1 == 1]
console.log(Math.abs("-1"));//1
console.log(Math.abs("-8"));//8
ceil() 向上取整
console.log(Math.ceil(2.00004));//3
console.log(Math.ceil(-2.00004));//-2
floor() 向下取整
console.log(Math.floor(2.9));//2
console.log(Math.floor(3.1));//3
console.log(Math.floor(-3.1));//3
max() 最大的
console.log(Math.max(10,20,30,55,2));//55
min() 最小的
console.log(Math.min(10,20,30,55,2));//2
random() 随机数
console.log(parseInt(Math.random()*10));
PI π
console.log(Math.PI);
pow() 2的4次方 222*2
console.log(Math.pow(2,4));
round() 四舍五入
console.log(Math.round(3.5415926));
自己定义一个函数 实现系统的max方法
<script>
function myMax(m){
var max = m[0];
for(var i = 1; i < m.length; i++){
if(max < m[i]){
max = m[i];
}
}
return max;
}
console.log(myMax([10,20,30,40,50]));
function myMax1(){
var max = arguments[0];
for(var i = 0; i < arguments.length;i++){
if(max < arguments[i]){
max = arguments[i];
}
}
return max;
}
console.log(myMax1(10,29,3,5,31,24));
</script>
自己定义一个对象 实现系统的max方法
function MyMath(){
//添加一个方法 获取最大值
this.getMax = function (){
var max = arguments[0];
for(var i = 0; i < arguments.length;i++){
if(max < arguments[i]){
max = arguments[i];
}
}
return max;
}
}
var mx = new MyMath();
console.log(mx.getMax(10,20,304,23,3423532,7));