zoukankan      html  css  js  c++  java
  • js的内部类

    JavaScript中本身提供一些,可以直接使用的类,这种类就是内部类。主要有:

    Object/Array/Math/Boolean/String/RegExp/Date/Number共8个内部类。

    内部类的分类:

    从使用方式,把js内部类分为两类(动态类,静态类)。

    静态类 使用  类名.属性|方法

    比如Math

    动态类 使用  var 对象=new 动态类() 对象.属性|方法

    //Math
        window.alert(Math.abs(-12));
        //Date
        //显示当前的日期
        var nowdate=new Date();
        window.alert(nowdate.toLocaleString());//按照一定格式显示。

    Math常用的函数

    1.abs(x)返回数的绝对值

    2.ceil(x)对一个数进行上舍入

    3.floor(x)对一个数进行下舍入

    4.max(x,y)求x,y中较大的数

    5.min(x,y)求x,y中较小的数

    6.round(x)对x进行四舍五入

    7.random()一个大于0小于1的16位小数位的数字。

    window.alert(Math.abs(-12));
        //Date
        //显示当前的日期
        var nowdate=new Date();
        window.alert(nowdate.toLocaleString());
    
        alert(Math.ceil(4.5));
        alert(Math.floor(4.5));
        alert(Math.round(4.7));
        alert(Math.round(4.49));
        alert(Math.random());
    
        alert(parseInt(Math.random()*100));
        alert(Math.round(Math.random()*100));

    js内部类--Date类

    //Date的常用方法
        //window.alert(new Date().toLocaleString());
    
        /*var date=new Date();
        window.alert(date);
        window.alert(date.getYear()+" "+date.getMonth());*/
    
        function showHello(date){
            //小时
            var hour=date.getHours();
            //分钟
            var minute=date.getMinutes();
    
            //1. 思路  把小时和分钟转成 距离00:00的秒数(小时数)
            //alert(new Date().toLocaleString());
            /*alert(hour);
            alert(minute);*/
    
            //把秒数考虑进去
            var mysecond=hour*3600+minute*60;
    
            if(mysecond>=6*3600&&mysecond<=9*3600){
                window.alert("早上好");
            }else if(mysecond>=17*3600+31*60&&mysecond<=18*3600+40*60){
                window.alert("傍晚好");
            }
    
            /*var nowTime=date.getHours()+date.getMinutes();
            if((nowHour>=6&&nowHour<9)||(nowHour==9&&minute==0)){
                window.alert("早上好");
            }else if(nowHour>=6&&nowHour<9){
    
            }*/

    String类

    String是动态类,提供了对字符串的各种操作,下面介绍几个常用的函数。

    split();分割字符串。

    substr(start,length):从位置start开始截取length个字符

    substring(start,end):从位置start开始截取到字符直到end-1结束。

    var str="abcd12345";
        window.alert(str.length);
        var str2="abc def oop";
        var arr=str2.split(" ");//如果是split("")则按字符分割。
        window.alert(arr);
    
        var str3="abcdef";
        window.alert(str3.substr(2,3));//cde
        window.alert(str3.substring(2,3));//c
    
        var str4="abcd";
        window.alert("charAt"+str4.charAt(3));

    运行结果:

    abc def oop

    cde

    c

    d

    indexOf的用法

    indexOf(str,start)

    从位置start开始寻找字符串str,默认情况下start是0(不写的时候),如果找到则返回字符串出现的位置,否则返回-1.

    String是一个动态类。

    var str5="ab 123 56 123 ab";
    window.alert("position:"+str5.indexOf("ab",1));//支持汉字查询

    返回14.

    Array类
    是动态类
    看看常用的方法:

    pop()删除一个元素并返回最后一个元素

    push()添加一个元素

    var myarr=new Array();
        window.alert(myarr.length);
        //动态地添加数据
        myarr[0]="sp";
        myarr[1]=90;
        window.alert(myarr.length+" "+myarr);
        //删除数组中的元素
        myarr.pop();
        window.alert(myarr.length+" "+myarr);
        myarr.push("abcd");
        window.alert(myarr.length+" "+myarr);

    输出结果:0

    2 sp,90

    1 sp

    2 sp,abcd

    
    

    var arr=new Array(2);
    window.alert(arr.length);
    arr[0]=1;
    arr[1]="a";
    arr[2]=2.3;
    window.alert(arr.length);
    window.alert(arr[3]);
    //arr[10]="hello";//不要跳过下标赋值,虽然不会报错并且在第10个位置插入了元素,但是不利于程序
    arr['a']="ok";
    window.alert(arr.length+" "+arr);
    window.alert(arr['a']);

     

    输出:

    2

    3

    undefined

    3 2,3,undefined

    ok

    虽然有arr['a']="ok";这条语句插入了一个元素,但是数组元素个数却没有增加。

    js中允许这样访问变量

    function Person(){
            this.name="xiao";
            this.show=function(){
                window.alert("hello");
            }
        }
        var p=new Person();
        document.writeln(p.name);
        document.writeln(p["name"]);
  • 相关阅读:
    致亲爱的304
    C语言中简单的for循环和浮点型变量
    C程序内存管理
    变量
    我哭了
    那一场邂逅
    如何修改安卓项目的图标
    Android requires compiler compliance level 5.0 or 6.0. Found '1.4' instead.解决方法
    Android 异步加载
    大家好,第一次用博客记录一些东西
  • 原文地址:https://www.cnblogs.com/liaoxiaolao/p/9768302.html
Copyright © 2011-2022 走看看