javascript - 内置对象 String/Date/Array/Math

1.构建对象的方法

<script>
//构建对象方法
//第1种
var people = new Object();
people.name = "iwen";
people.age = 26;
document.write("name:" + people.name + ",age:" + people.age + "<br>"); //第2种
people1 = { name: "zhangsan", age: 27 };
document.write("name:" + people1.name + ",age:" + people1.age + "<br>"); //第3种
function peopleConstructor(name, age) {
this._name = name;
this._age = age;
}
son = new peopleConstructor("liuz", 28); document.write("name:" + son._name + ",age:" + son._age + "<br>");
</script>

javascript - 内置对象 String/Date/Array/Math


2.String字符串对象

1.查找:indexOf()

2.内容匹配:match()

        var str = "Hello World";
//匹配
document.write(str.match("World")+"<br>");
document.write(str.match("world"));

javascript - 内置对象 String/Date/Array/Math

 

3.替换:replace()

4.大小写转换:toUpperCase() \ toLowerCase()

      //转换大写
str=str.toUpperCase();
document.write(str + "<br>");
//转换小写
str = str.toLowerCase();
document.write(str + "<br>");

javascript - 内置对象 String/Date/Array/Math

 

5.字符串转为数组:split()

var str1 = "Hello Bai du";
//document.write(str1 + "<br>");
var s = str1.split(" ");
for (var i = 0; i < s.length; i++) {
document.write(s[i] + "<br>");
}

javascript - 内置对象 String/Date/Array/Math

 


3.Date日期对象

getFullYear():获取年份

getTime():获取毫秒

setFullYear():设置具体日期

getDay():获取星期

javascript - 内置对象 String/Date/Array/Math

<div id="timetxt">
</div>
<script type="text/javascript">
var date = new Date();
document.write(date);
document.write("<br>");
//获取年份
document.write(date.getFullYear());
document.write("<br>");
//获取毫秒
document.write(date.getTime()); startTime(); function startTime() {
var today = new Date();
var h = today.getHours();
var m = checkTime(today.getMinutes());
var s = checkTime(today.getSeconds()); document.getElementById("timetxt").innerHTML = h + ":" + m + ":" + s;
setTimeout(function () {
startTime();
}, 2000);
} function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
} </script>

 


4.Array数组对象

concat():合并数组

sort():排序

push():末尾追加元素

reverse():数组元素翻转

javascript - 内置对象 String/Date/Array/Math

        document.write('--------------合并-------------<br>');
var a = ["hello", "world"];
var b = ["tang", "sansan"];
var c = a.concat(b);
document.write("c:" + c); document.write('<br>---------------排序------------<br>');
var d = ["a", "c", "d", "t", "b", "e"];
d.sort();
document.write(d); document.write('<br>--------------末尾追加-----------<br>');
var e = ["hello", "world"];
e.push("tangsansan");
document.write(e); document.write('<br>---------------翻转------------<br>');
c.reverse();
document.write(c);

 


5.Math对象

算数任务

round():四舍五入

random():返回 0 ~ 1 之间的随机数

max():返回最高值

min():返回中的最低值

abs():绝对值

javascript - 内置对象 String/Date/Array/Math

        var num = 2.5;
document.write('---------------Math.round------------<br>');
document.write(Math.round(num));
document.write('<br>---------------1~100的随机数------------<br>');
document.write(parseInt(Math.random() * 100));
document.write('<br>---------------最大值------------<br>');
document.write(Math.max(num, 23, 52, 77, 32));
document.write('<br>---------------最小值------------<br>');
document.write(Math.min(num, 23, 52, 77, 32));
document.write('<br>--------------绝对值------------<br>');
var abs1 = 10;
var abs2 = -10;
document.write(Math.abs(abs1));
document.write('<br>');
document.write(Math.abs(abs2));
上一篇:iframe父页面和子页面相互调用的方法


下一篇:20165326 java第五周学习笔记