您的位置:首页 > Web前端 > JavaScript

JavaScript Math 对象总结

2016-07-08 13:52 435 查看
Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math(),像 Math.sin() 这样的函数只是函数,不是某个对象的方法。您无需创建它,通过把 Math 作为对象使用就可以调用其所有属性和方法。

Math.random(); //返回介于 0 ~ 1 之间的一个随机数。

//document.write(Math.random());
function SetRandom(min,max){//生成固定范围内的随机数
return min+parseInt(Math.random()*(max-min))
}
document.write(SetRandom(5,15));


Math.round(数据); //四舍五入取整 向大(上、左)、小(下、右)

//document.write(Math.round(3.5));  //4
//document.write(Math.round(-3.5)); //-3
//document.write(Math.round(-3.4)); //-3
//document.write(Math.round(-3.6)); //-4
//document.write(Math.round(3.499));//3


Math.abs(数据); //求绝对值 干掉负号

//document.write(Math.abs(-3.00000001));    //3
//document.write(Math.abs(7.25-10))         //2.75
//document.write(Math.abs(-3,4));           //3     4被忽略了


Math.ceil(数据); 向上(大,左)

//document.write(Math.ceil(3.4));   //4
//document.write(Math.ceil(-3.4));  //3
//document.write(Math.ceil(3.499)); //4
//document.write(Math.ceil(3.0000000001));  //4
//document.write(Math.ceil(3.000000000));   //3


Math.floor(数据); 向下

//document.write(Math.floor(3.0000000001)); //3
//document.write(Math.floor(3.49));         //3
//document.write(Math.floor(3.59));         //3
//document.write(Math.floor(-3.49));        //-4
//document.write(Math.floor(-3.00000001));  //-4
//document.write(Math.floor(-3.0000000));   //-3


Math.sqrt(数据); 根号

//document.write(Math.sqrt(-2));    //NaN
//document.write(Math.sqrt(2.2));   //


Math.pow(x,y); 返回 x 的 y 次幂

//document.write(Math.pow(2,3));    //2*2*2 ==  8
//document.write(Math.pow(-2,4));


Math.max(a,b)/Math.min(a,b) 取最大、最小值

//document.write(Math.max(2,3));        //3
//document.write(Math.max(2,3,5,11));   //3 11  可以接受多个参数
//document.write(Math.max(2,3,[10]));   //10    number
//document.write(Math.max(2,3,[10,11]));    //NaN
//document.write(Math.max(2,3,true));   //3 NaN     true    ==  1
//document.write(Math.max(2,3,'11'));   //  11


Math.sin(x); //返回一个数字的正弦。参数 x 的正弦值。返回值在 -1.0 到 1.0 之间。

//一个以弧度表示的角。将角度乘以 0.017453293 (2PI/360)即可转换为弧度。

//document.write(Math.sin(3)); //0.1411200080598672
//document.write(Math.sin(-3)); //-0.1411200080598672
//document.write(Math.sin(0)); //0
//document.write(Math.sin(Math.PI)); //1.2246063538223772e-16
//document.write(Math.sin(Math.PI/2); //1


Math.cos(x) //返回一个数字的余弦值。

//x 的余弦值。返回的是 -1.0 到 1.0 之间的数。

//document.write(Math.cos(3)); //-0.9899924966004454
//document.write(Math.cos(-3));//-0.9899924966004454
//document.write(Math.cos(0));//1
//document.write(Math.cos(Math.PI));//-1
//document.write(Math.cos(2*Math.PI));//1


Math.tan(x);//返回一个表示某个角的正切的数字。

//一个以弧度表示的角。将角度乘以 0.017453293 (2PI/360)即可转换为弧度。

//document.write(Math.tan(0.50)); //0.5463024898437905
//document.write(Math.tan(-0.50)); //-0.5463024898437905
//document.write(Math.tan(5)); //-3.380515006246586
//document.write(Math.tan(10)); //0.6483608274590866
//document.write(Math.tan(-5)); //3.380515006246586
//document.write(Math.tan(-10)); //-0.6483608274590866


Math.acos(x); 返回一个数的反余弦 x必须是 -1.0 ~ 1.0 之间的数。

//x 的反余弦值。返回的值是 0 到 PI 之间的弧度值

//如果参数 x 超过了 -1.0 ~ 1.0 的范围,那么浏览器将返回 NaN。

//document.write(Math.acos(0.64)); //0.8762980611683406
//document.write(Math.acos(0));  //1.5707963267948965
//document.write(Math.acos(-1)); //3.141592653589793
//document.write(Math.acos(1)); //0
//document.write(Math.acos(2)); //NaN


Math.asin(x); 返回一个数的反正弦值 x必须是 -1.0 ~ 1.0 之间的数。

//x 的反正弦值。返回的值是 -PI/2 到 PI/2 之间的弧度值。

//如果参数 x 超过了 -1.0 ~ 1.0 的范围,那么浏览器将返回 NaN。

//document.write(Math.asin(0.64)); //0.6944982656265559
//document.write(Math.asin(0));  //0
//document.write(Math.asin(-1));  //-1.5707963267948965
//document.write(Math.asin(1)); //1.5707963267948965
//document.write(Math.asin(2)); //NaN


Math.atan(x); 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。

//document.write(Math.atan(0.50)); //0.4636476090008061
//document.write(Math.atan(-0.50)); //-0.4636476090008061
//document.write(Math.atan(5)); //1.373400766945016
//document.write(Math.atan(10)); //1.4711276743037347
//document.write(Math.atan(-5)); //-1.373400766945016
//document.write(Math.atan(-10)); //-1.4711276743037347


Math.atan2(y,x); //返回从 x 轴到点 (x,y) 之间的角度。

//document.write(Math.atan2(0.50,0.50)); //0.7853981633974483
//document.write(Math.atan2(-0.50,-0.50)); //-2.356194490192345
//document.write(Math.atan2(5,5)); //0.7853981633974483
//document.write(Math.atan2(10,20)); //0.4636476090008061
//document.write(Math.atan2(-5,-5)); //-2.356194490192345
//document.write(Math.atan2(-10,10)); //-0.7853981633974483


object.toSource(); //返回表示对象源代码的字符串。

//注释:该方法在 Internet Explorer 中无效。

function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}

var bill=new employee("Bill Gates","Engineer",1985);

document.write(bill.toSource());


//输出:({name:"Bill Gates", job:"Engineer", born:1985})
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: