内置对象

js三种对象
1- 内置对象
2- 自定义对象
3- 浏览器对象 BOm

绝对值[1 == 1][-1 == 1]

console.log(Math.abs("-1"));//1
console.log(Math.abs("-8"));//8

ceil() 向上取整

console.log(Math.ceil(2.0003));//3
console.log(Math.ceil(-2.0003));//-2

floor() 向下取整

console.log(Math.floor(2.9));//2
console.log(Math.floor(3.1));//3
console.log(Math.floor(-3.1));//-4

max() 最大的

console.log(Math.max(10,20,30,55,3));//55

min() 最小的

console.log(Math.min(10,20,30,55,3));//3

random() 随机数

console.log(parseInt(Math.random()*10));

PI π 圆周率

console.log(Math.PI);

pow() 2的4次方

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]));
</script>
<script>
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,20,3,5,31,24));
</script>

自己定义一个对象 实现系统的max方法

<script>
	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,30,35,5232,1));
</script>
上一篇:JAVAScript 中的函数定义和调用


下一篇:用es3的语法实现bind