09.JavaScript内置对象

来源:互联网 发布:华讯网络 怎么样 编辑:程序博客网 时间:2024/06/06 03:38

什么是JavaScript对象

对象是JavaScript中最重要的API
JavaScript包含多种对象

  • 内置对象
  • 外部对象
    • window对象
    • dom对象
  • 自定义对象

如何使用对象

  • 对象包含属性和方法
  • 访问对象属性
    – 对象.属性
  • 访问对象方法
    – 对象.方法名()

常用内置对象

JS中常用内置对象

1.String对象

  • 创建String对象

    a) var str1 = “hello world”;

    b) var str2 = new String(“hello world”);

  • String对象属性:length

    str1.length

  • 常用方法:

    • 大小写转换

      x.toLowerCase()

      x.toUpperCase()

    • 获取指定字符:

      x.charAt(index):返回指定位置的字符

      x.charCodeAt(index):返回指定位置字符的Unicode编码

    • 其他同Java

2.Number对象:是数值对象

  • 创建对象

    var n = 3.1415926;

  • 常用方法:

    toFixed(num),z=转换为字符串,并保留小数点后一定尾数,位数不够用0补足

    n.toFixed(3) —-> 3.142

3.Boolean对象

4.Array对象:

JS中的数组都是Object[]

  • 创建数组

    • a)已知数据:
      var a = [“one”, 25, true];
      a[0] —-> one
    • b) 未知数据:
      var b = new Array();
      b.push(“two”);
      b.push(“23”);
      b.push(false); —-> two 23 false
  • 数组倒转(不按照大小排序)

    x.reserve()

  • 数组排序

    x.sort( function(a, b) { return a-b; } )

5.Math对象

与java中Math方法类似

6.Date对象

  • 创建对象:

    var d1 = new Date(); —> 获取服务器/数据库时间

    var d2 = new Date( “2008/8/8 08:08:08” ); —> 固定时间

  • 转换为本地时间格式

    d1.toLocaleDateString(); —> 获取日期

    d2.toLocaleTimeString(); —> 获取时间

  • 读取时间分量

    var y = d1.getFullYear();

    var m = d2.getMonth(); —> 月份从0开始

    var d = d1,getDate();

7.RegExp对象

  • 如何创建正则表达式
    直接量:/正则/模式

    实例化:new RegExp(正则字符串,模式字符串)

    var str = “you can you up, no can no bb”;

    var reg = /No/i; //g:全局,存在也继续,i忽略大小写

  • 正则对象的方法

    • test()检测str中是否和reg匹配的字符串

      reg.test(str) —> true

    • exec()

      在非全局模式下:从str中账户和reg匹配的第一个字符串

          reg.exec(str) —> “no” index:16, input….

      在全局模式下:第1次调用,从str找出和reg匹配的第一个字符串

          第n次调用,从str找出和reg匹配的第n个字符串

    var reg = /no/g; //功能,在页面中搜索
    reg.exec(str) —>index:16
    reg.exec(str) —>index:23
    reg.exec(str) —>null
    reg.exec(str) —>index:16 //返回第一次位置

8.Function对象

  • JS中函数就是Function对象
  • 函数名就是只想Function对象的引用
  • JS的函数没有重载
  • 调用时只要函数名一样,无论传入多少个参数,调用的都是同一个函数
  • 所有的参数传递给arguments数组对象

9.arguments对象

  • 在函数中表示函数的参数数组,
  • 在函数对象只可以利用arguments实现可变参数的函数

    function sum() {    var s = 0;    if(arguments.length>0) {        for(var i=0; i<arguments.length; i++) {            s += arguments[i];        }    }    return s;}console.log(sum(1,2));              --->3console.log(sum(3,4,5,6));          --->18

case:log in

功能实现:账号密码符合规范,显示绿色,不符合则显示红色

<style>    .ok{ color:green; }    .error{ color:red; }</style><script>    function checkCode() {        //获取账号        var input = document.getElementById(“account”).value;        //获取span        var span = document.getElementById(“account_message”);        //验证账号格式        var reg = /^\w{5,10}$/;        if(reg.test(input)) {            span.className = “ok”;            return true;                    //onsubmit事件        }else {            span.className=”error”;            return fasle;                   //onsubmit事件        }    }    function checkPsw() {        var input = document.getElementById(“password”).value;        var span = document.getElementById(“password_message”);        var reg = /^\w{10,15}$/;        if(reg.test(input)) {            span.className = “ok”;          //onsubmit事件            return true;        }else {            span.className = “erroe”;            return false;                   //onsbumit事件        }    }</script><body>    <!-- onsubmit=”return checkCode()+checkPwd()==2” -->    <form action=”www.albertliangzt.com” onsubmit=”alert((checkCode)&&checkPwd())”>    <p>        Account:        <input type=”text” onblur=”checkCode();” id=”account”/>        <span id=”account_message”>5-10 letter, number, underline</span>    </p>    <p>        Password:        <input type=”password” onblur=”checkPsw();” id=”password”>        <span id=”password_message”>10-15 letter, number, underline</span>    </p>    <p>        <input type=”submit” value=”log in”>    </p></body>

全局函数可用于所用的JavaScript对象

常用的全局函数有:

  • parseInt/parseFloat
  • isNaN
  • eval

case:simple calculate

功能实现:文本框内输入计算式,点击按钮计算,并将结果显示在文本框

<script>    function calculate() {        //获取文本框        var input = document.getElementById(“num”);         //获取文本框内的算式         var exp = input.value;        //计算        try{            var result = eval(exp);            input.value = result;        }catch(e) {            input.value = “Error”;        }    }</script>    <body>    <input type=”text” id=”num”/>    <input type=”button” value=”=” onclick=”calculate();”></body>
0 0
原创粉丝点击