0
点赞
收藏
分享

微信扫一扫

JavaScript 日期、Math对象 及自定义对象

JavaScript对象这部分是特别容易学会的 前提是基于Java基础。

日期对象:
通过new Date创建一个日期对象,这个对象就表示当前日期(现在)
分别获取年/月/日
需要注意的是,getMonth()返回的月数,是基零的,0代表1月份
getDay() 也是如此

var d = new Date();
  document.write('分别获取年月日: ');
  document.write(d.getFullYear());
  document.write("/");
  document.write(d.getMonth()+1);//加1是修饰一下 可读性
  document.write("/");
  document.write(d.getDate());//但是这个不会作修饰

  document.write("分别获取时:分:秒:毫秒 ");
  document.write(d.getHours());
  document.write(":");
  document.write(d.getMinutes());
  document.write(":");
  document.write(d.getSeconds());
  document.write(":");
  document.write(d.getMilliseconds());
  //如同Java中的gettime() 是获取从1970/1/1 08:00:00 至今的毫秒数
  //每一个的方法名 get改为set 即是设置自定义时间

Math是JavaScript的工具对象,用于常见的数学运算

document.write(Math.E);//Math 进行引用 E为自然对数

document.write(Math.PI);//圆周率

document.write(Math.abs(-1));//取绝对值

方法 min max 分别取最小值,最大值
方法 pow 求一个数的n次方
document.write(Math.pow(3,3)); //3的立方,即27
方法 round,小数四舍五入取整

取随机数:
document.write("一个 0-1 之间的随机数 : Math.random():");//random()函数默认是0-1的随机数 要实现5到10的 需要式子变换

自定义对象:

JavaScript没有类相比Java ,只有设计对象 那么对象应包含属性 方法

一般为:
some=new Object();
some.shuxing1="";
some.fangfa1=function(){
}

也可以这样
function make(name)
{
this.name=name;
this.fangfa1=function(){}
}
p1=new make("sd");
p1.fangfa1();
举报

相关推荐

0 条评论