JS-day2[对象,函数]

JavaScript中的对象

<script language="javascript">
var aa=Number.MAX_VALUE; 
//利用数字对象获取可表示最大数
var bb=new String("hello JavaScript"); 
//创建字符串对象
var cc=new Date();
//创建日期对象
var dd=new Array("星期一","星期二","星期三","星期四"); 
//数组对象
</script>

  使用new 来实例化生成一个对象。

 

字符串对象

  字符串对象创建

    1.变量 = "字符串"

    2.字符串对象名称 = new String(字符串)

 字符串对象的属性和函数

x.length         ----获取字符串的长度

 x.toLowerCase()        ----转为小写
 x.toUpperCase()        ----转为大写
 x.trim()               ----去除字符串两边空格       


----字符串查询方法

x.charAt(index)         ----str1.charAt(index);----获取指定位置字符,其中index为要获取的字符索引

x.indexOf(findstr,index)----查询字符串位置
x.lastIndexOf(findstr)  

x.match(regexp)         ----match返回匹配字符串的数组,如果没有匹配则返回null
x.search(regexp)        ----search返回匹配字符串的首字符位置索引

                        示例:
                        var str1="welcome to the world of JS!";
                        var str2=str1.match("world");
                        var str3=str1.search("world");
                        alert(str2[0]);  // 结果为"world"
                        alert(str3);     // 结果为15
                        

----子字符串处理方法

x.substr(start, length) ----start表示开始位置,length表示截取长度
x.substring(start, end) ----end是结束位置

x.slice(start, end)     ----切片操作字符串
                        示例:
                            var str1="abcdefgh";
                            var str2=str1.slice(2,4);
                            var str3=str1.slice(4);
                            var str4=str1.slice(2,-1);
                            var str5=str1.slice(-3,-1);

                            alert(str2); //结果为"cd"
                            
                            alert(str3); //结果为"efgh"
                            
                            alert(str4); //结果为"cdefg"
                            
                            alert(str5); //结果为"fg"

x.replace(findstr,tostr) ----    字符串替换

x.split();                 ----分割字符串
                                 var str1="一,二,三,四,五,六,日"; 
                                var strArray=str1.split(",");
                                alert(strArray[1]);//结果为"二"
                                
x.concat(addstr)         ----    拼接字符串

数组对象

数组创建:

        //创建方式1
        var n=[1,2,3,4,5,6,7];
        console.log(n);

        //创建方式2
        var n1=new Array(8,9,7,8,9,10);
        console.log(n1);

        //创建方式3
        var n2=new Array(7);     //初始化数组对象
            n2[0]="星期日";
            n2[1]="星期一";
            n2[2]="星期二";
            n2[3]="星期三";
            n2[4]="星期四";
            n2[5]="星期五";
            n2[6]="星期六";
        console.log(n2[0]);;

数组的属性和方法:

join:

  将数组元素拼接成字符串

        var n=[1,2,3,4,5,6,7]
        var str1=n.join("-")    //括号中可以指定连接符
        console.log(str1)
        console.log(typeof str1)  
执行结果:
1-2-3-4-5-6-7
string

concat:

  连接一个或者多个数组,并不会改变原有的数组。

        var a=[1,2,3];
        var b=a.concat(4,5);
        console.log(a);    //结果为1,2,3
        console.log(b);    //结果为1,2,3,4,5

reverse:

  反转。

        var a = [10, 2, 55, 382];
        a.reverse();
        console.log(a);  //[382, 55, 2, 10]

sort:

  排序。默认按照ascii码大小规则排序。

        var a = [10, 2, 55, 382];
        a.sort();
        console.log(a);  //[10, 2, 382, 55]
 按照数字大小排列,而非ascii
    arr = [382,20];

    function intsort(a,b) {
        return a-b;    //从小到大排序    改成 b-a就是从大到小
    }
    arr.sort(intsort)
    console.log(arr)

slice:

  x.slice[开始位置,结束位置]   顾头不顾尾

    var a=[1,2,3,4,5,6,7,8];
    var b=a.slice(2,4);    //[3, 4]
    console.log(b);
    var c=a.slice(3,-2);   //[4, 5, 6]
    console.log(c);
    var d=a.slice(5);  //不写结束位置就直接从开始位置到接收
    console.log(d);       //[6, 7, 8]  

splice:

  删除数组,插入数值

    var a = [1, 2, 3, 4, 5, 6];
    a.splice(1, 3);  //删除索引1-3位置的数值
    console.log(a);  // [1, 5, 6]
    var a = [1, 2, 3, 4, 5, 6];
    a.splice(1, 1);   //删除索引1位置的数值
    console.log(a); //  [1, 3, 4, 5, 6]
    var a = [1, 2, 3, 4, 5, 6];
    a.splice(1, 0, 10, 11, 12, 45)   //删除索引1,并插入数值
    console.log(a)   //[1, 10, 11, 12, 45, 2, 3, 4, 5, 6]

push和pop:

  push:在数组结尾添加

  pop:删除数组结尾的数值

    var a=[1,2,3];
    a.push(4,5,6,7);
    console.log(a);  //[1, 2, 3, 4, 5, 6, 7]
    a.pop();
    console.log(a); //[1, 2, 3, 4, 5, 6]

shift和unshift:

  unshift:将数值插入数组的开头

  shift:删除数组的第一个数值

    var a=[1,2,3];
    a.unshift(4,5,6,7);
    console.log(a);  //[4, 5, 6, 7, 1, 2, 3]
    a.shift();
    console.log(a);   //[5, 6, 7, 1, 2, 3]

date对象:

//创建date对象
//方法1:不指定参数
var nowd1=new Date();
alert(nowd1.toLocaleString( ));
//方法2:参数为日期字符串
var nowd2=new Date("2004/3/20 11:12");
alert(nowd2.toLocaleString( ));
var nowd3=new Date("04/03/20 11:12");
alert(nowd3.toLocaleString( ));
//方法3:参数为毫秒数
var nowd3=new Date(5000);
alert(nowd3.toLocaleString( ));
alert(nowd3.toUTCString());

//方法4:参数为年月日小时分钟秒毫秒
var nowd4=new Date(2004,2,20,11,12,0,300);
alert(nowd4.toLocaleString( ));//毫秒并不直接显示

//date对象获取时间
获取日期和时间
getDate()                 获取日
getDay ()                 获取星期
getMonth ()               获取月(0-11)
getFullYear ()            获取完整年份
getYear ()                获取年
getHours ()               获取小时
getMinutes ()             获取分钟
getSeconds ()             获取秒
getMilliseconds ()        获取毫秒
getTime ()                返回累计毫秒数(从1970/1/1午夜)

//date时间转换
日期和时间的转换:

getTimezoneOffset():8个时区×15度×4分/度=480;
返回本地时间与GMT的时间差,以分钟为单位
toUTCString()
返回国际标准时间字符串
toLocalString()
返回本地格式时间字符串
Date.parse(x)
返回累计毫秒数(从1970/1/1午夜到本地时间)
Date.UTC(x)
返回累计毫秒数(从1970/1/1午夜到国际时间)

Math对象

//该对象中的属性方法 和数学有关.
   

abs(x)    返回数的绝对值。
exp(x)    返回 e 的指数。
floor(x)对数进行下舍入。
log(x)    返回数的自然对数(底为e)。
max(x,y)    返回 x 和 y 中的最高值。
min(x,y)    返回 x 和 y 中的最低值。
pow(x,y)    返回 x 的 y 次幂。
random()    返回 0 ~ 1 之间的随机数。
round(x)    把数四舍五入为最接近的整数。
sin(x)    返回数的正弦。
sqrt(x)    返回数的平方根。
tan(x)    返回角的正切。

//方法练习:
        //alert(Math.random()); // 获得随机数 0~1 不包括1.
        //alert(Math.round(1.5)); // 四舍五入
        //练习:获取1-100的随机整数,包括1和100
             //var num=Math.random();
             //num=num*10;
             //num=Math.round(num);
             //alert(num)
        //============max  min=========================
        /* alert(Math.max(1,2));// 2
        alert(Math.min(1,2));// 1 */
        //-------------pow--------------------------------
        alert(Math.pow(2,4));// pow 计算参数1 的参数2 次方.

function对象

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>

        var ID;   //设置变量

        function start() {
            if (ID == undefined) {    //判断如果ID等于undefined
                foo(); 
                ID = setInterval(foo, 1000);   //每隔一秒运行一次foo函数
            }
        }

        function foo() {
            var timer = new Date().toString();    //实例化时间对象
            var ele = document.getElementById("time");    //通过id获取input text文本框对象
            ele.value = timer;    //设置属性 value=获取的时间
        }

        function stop() {
            clearInterval(ID);    //停止setinterval运行
            ID = undefined;    //重新赋值ID为undefined

        }


    </script>
</head>
<body>
<input type="text" id="time">   //定义一个文本框 设置id
<button onclick="start()">start</button>   //设置一个按钮 点击运行函数start
<button onclick="stop()">stop</button>   //点击运行函数stop
</body>
</html>

函数内置对象arguments

function add(a,b){

        console.log(a+b);//3
        console.log(arguments.length);//2
        console.log(arguments);//[1,2]

    }
    add(1,2)

    ------------------arguments的用处1 ------------------
    function nxAdd(){
        var result=0;
        for (var num in arguments){
            result+=arguments[num]
        }
        alert(result)

    }

    nxAdd(1,2,3,4,5)

//     ------------------arguments的用处2 ------------------

    function f(a,b,c){
        if (arguments.length!=3){
            throw new Error("function f called with "+arguments.length+" arguments,but it just need 3 arguments")
        }
        else {
            alert("success!")
        }
    }

    f(1,2,3,4,5)
View Code

匿名函数

// 匿名函数
    var func = function(arg){
        return "tony";
    }

// 匿名函数的应用
    (function(){
        alert("tony");
    } )()

    (function(arg){
        console.log(arg);
    })('123')
View Code

 

posted @ 2017-08-08 19:32  neuropathy_ldsly  阅读(117)  评论(0)    收藏  举报