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

JavaScript—内置对象

2016-05-02 14:36 513 查看
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>标签</title>
</head>
<body onload="startTime()">
<!--创建对象-->

<script>
<!--创建方式1-->
//    people = new Object();
//    people.name = "iwen";
//    people.age = "30";
//    document.write("name:"+people.name+",age:"+people.age);

//创建方式2:
//    people = {name:"iwen",age:"30"};
//    document.write("name:"+people.name+",age:"+people.age);

//使用函数创建对象
//    function people(name,age){
//        this.name = name;
//        this.age = age;
//    }
//    son = new people("iwen","20");
//    document.write("name:"+son.name+",age:"+son.age);

//    var str = "hello world";
//    var str1 = "today is monday";
//    document.write(str.length);
//    document.write(str.indexOf("hello"));
//
//    document.write(str.match("hello1"));
//
//    document.write(str.replace("world","you are mine"));  // 第一个是原本的,第二个是替换过后的参数
//
//    document.write(str.toUpperCase());

//    将字符串转换为数组
//    var s = str1.split(" ");  // 以空格来作为数组转换为数组的标志
//    document.write(s[1]);

//    document.write(str.prototype);

var date = new Date();
//        document.write(date);
//        document.write(date.getFullYear());  // 获取年份
//        document.write(date.getTime()); // 获取时间戳,精确到毫秒
//        date.setFullYear(2012,1,1); // 自己设定时间
//        document.write(date);
//          document.write(date.getDay()); // 获取星期

// 显示当前的小时,分钟,秒
//        function startTime(){
//            var today = new Date();
//            var h = today.getHours();
//            var m = today.getMinutes();
//            var s = today.getSeconds();
//            m = checkTime(m);
//            s = checkTime(s);
//            document.getElementById("timetxt").innerHTML = h+":"+m+":"+s;
//            // 让日期每500毫秒刷新一次,设置递归函数
//            t = setTimeout(function(){
//                startTime();
//            },500);
//        }
//        // 由于秒和分钟有个位数和两位数,因此进行判断,记得在上方调用函数 startTime()
//        function checkTime(i){
//            if (i < 10){
//                i = "0"+i;
//
//            }return i;
//       }

// 数组的元素操作
//        var a = ["hello","world"];
//        var b = ["iwen","ime"];
//         // 合并数组元素
//        var c = a.concat(b);
//        document.write(c);

//        var a = ["a","b","c","d","e"];
//        document.write(a.sort());  // 进行排序
//        var a = ["2","3","5","1","4"];
//        document.write(a.sort(function(a,b){
//            return a - b; // 意思是从a 到 b,   进行升序排列;  如果是从b 到a ,就是降序排列
//        }));

//        var a = ["a","b"];
//        document.write(a.push("c")); //追加元素,也就是给数组末尾添加一个元素
//        document.write(a.reverse());  //  反转数组元素排列

// Math 对象
/*
round(): 四舍五入
random(): 返回0~1之间的随机数
max():返回最高值
min(): 返回最低值
abs(): 返回绝对值
*/

document.write(Math.round(2.8)+"<br/>");
document.write(Math.random()+"<br/>");
document.write(Math.min("1","22","4","12")); //最大值和最小值

</script>
<div id = "timetxt"></div>

</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: