JS 04 Date_Math_String_Object
程序员文章站
2022-03-20 20:10:33
Date Math String Object ......
date
<script> //1.date对象 var d1 = new date(); //thu may 02 2019 14:27:19 gmt+0800 (中国标准时间) console.log(d1); //转成2019/5/2 下午2:27:19 console.log(d1.tolocalestring()); //距离1900年经过年数 console.log(d1.getyear());//119 //获取当前纪年 console.log(d1.getfullyear());//2019 //获取一年中当前月份 console.log(d1.getmonth()); //4, 0-11 //获取一个月中的日期 console.log(d1.getdate()); //时 console.log(d1.gethours()); //分 console.log(d1.getminutes()); //秒 console.log(d1.getseconds()); //设置月份时,记得值为0-11 var d2 = new date(2012,11,12,12,12,12); console.log(d2.tolocalestring());//2012/12/12 下午12:12:12 //可以创建一个距离1970年经过的毫秒数 var d3 = new date(1528980372222); console.log(d3.tolocalestring()) </script>
math
<script > console.log(math.random()) // [0,1) 随机数 console.log(math.abs(-5)) //求取绝对值 console.log(math.round(4.5)); //四舍五入 console.log(math.ceil(3.1)); //天花板 console.log(math.floor(3.9)); //地板 console.log(math.pow(2,20)); //x的y次幂 console.log(math.pi); //圆周率 </script>
string
<script type="application/javascript"> //var str = new string("aabbcc"); var str2 = "aabdcbc"; //console.log(str); console.log(str2); //转化大小写 console.log(str2.touppercase()); //大写 console.log(str2.tolowercase()); //小写 //返回指定索引处的字母 console.log(str2.charat(3)); //返回指定字母首次出现的索引 console.log(str2.indexof("b")); //返回指定字母最后一次出现的索引 console.log(str2.lastindexof("b")); //截取子字符串,从x位置截取y个字符 console.log(str2.substr(2,3)); //截取子字符串,从x位置截到y位置 console.log(str2.substring(2,3)); //字符串length属性表示字符的个数 console.log(str2.length) </script>
object
<script type="application/javascript"> //1.在js中,我们可以使用object对象来表述一个人 var obj = new object(); //2.在js中,对象有个特点:属性可以动态的添加 //添加属性 obj.name = "laowang"; //修改属性的值 obj.name = "laoma"; //删除属性 delete obj.name; function factory(name,age){ var obj = new object(); obj.name = name; obj.age = age; return obj; } var per1 = factory("laowang",30); console.log(per1.name); var c1 = factory("benz",3); console.log(c1.name); //封装的人“类” function person(name,age){ this.name = name; this.age = age; } var p1 = new person("zhangsan",20); console.log(p1.name); console.log(p1.age); var p2 = new person("乔峰",25); console.log(p2.name); console.log(p2.age); p2.skill = "降龙十八掌"; console.log(p1.skill); console.log(p2.skill); //为所有对象都添加属性 person.prototype.money = 8888; person.prototype.like = ["吃饭","睡觉","打豆豆"]; person.prototype.eat = function(){ alert("吃饭了,好开心!"); } console.log(p1.money); console.log(p2.money); p1.like.push("写java"); console.log(p1.like); console.log(p2.like); p1.money = 88888; console.log(p1.money); console.log(p2.money); function student(){ this.sno = math.random(); } student.prototype = new person("lisi",8); var s1 = new student(); console.log(s1.name); console.log(s1.age); console.log(s1.sno); console.log(s1.money); console.log(s1.like) </script>