JavaScript基础一

内置对象

 

一、String对象

  •  创建方式

    var str1 = "hello world";
    var str2 = new String("hello world");
  •  格式编排方法

    var str1 = "hello world";
    document.write(str1.italics()) //相当于在HTML中<i>hello world</i>
  • 大小写转换

    var str1 = "Hello World";
    document.write(str1.toLowerCase());
    document.write(str1.toUpperCase());
  • 获取指定字符串

    var str1 = "Hello World";
    document.write(str1.charAt(4));       //返回索引所在的字符
    document.write(str1.charCodeAt(4));   //返回索引所在字符的Unicode编码
  • 查询字符串

    var str1 = "Hello World";
    document.write(str1.indexOf("l"));       //返回第一个被查询到的字符索引值
    document.write(str1.lastIndexOf("l"));   //该结果与indexOf的查询顺序相反
    var str = "welcome to the China";
    var str1 = str.match("in")    //返回匹配字符串的数组,如果没有匹配则返回null
    var str2 = str.search("in")   //返回匹配字符串的首字符位置索引
    document.write(str1[0]);      //结果为:in
    document.write(str2);         //结果为:17
  • 字符串替换

    var str1 = "welcome to the China";
    var str2 = str1.replace("China","Japan");
    document.write(str2);           //结果为:welcome to the Japan
  • 截取字符串

    var str = "welcome to the China";
    var str1 = str.substr(2, 4);
    var str2 = str.substring(2, 4);
    document.write(str1);   //输出结果为:lcom
    document.write(str2);   //输出结果为:lc
  • 字符串切片

    var str = "welcome to the China";
    var str1 = str.slice(2,4);
    var str2 = str.slice(2);
    var str3 = str.slice(6,-1);
    var str4 = str.slice(-3,-1)
    document.write(str1);   //输出结果为:lc
    document.write(str2);   //输出结果为:lcome to the China
    document.write(str3);   //输出结果为:e to the Chin
    document.write(str4);   //输出结果为:in
  • 分隔字符串

    var str = "welcome to the China";
    var strArray = str.split(" ");
    document.write(strArray[0]);  //输出为:welcome
    document.write(strArray[1]);  //输出为:to
  • 拼接字符串

    var str1 = "welcome to the ";
    var str2 = str1.concat("China")
    document.write(str2);  //输出为:welcome to the China

 

二、Array对象

  • 二维数组

    var attr = new Array(7);
    for (var i = 0; i < 7; i++) {
        attr[i] = new Array();
    }
    attr[0][0] = "第一行第一列";
    attr[0][1] = "第一行第二列";

    attr[3][0] = "第三行第一列";
    attr[3][1] = "第三行第二列";
    attr[3][2] = "第三行第三列";

    attr[0][1] = "第一行第二列";
    attr[6][0] = "第七行第一列";
    attr[6][1] = "第七行第二列";
    console.log(attr)
  •  join方法

    //Python的字符串在使用join函数时,列表元素必须是字符串,而js对此无要求   
    var attr = [1,2,3,4];
    console.log(attr.join("--"))  //输出为:1--2--3--4
  • concat方法与toString方法

    var attr1 = [1,2,3,4];
    var attr2 = attr1.concat(5,6)
    console.log(attr1.toString())  //输出为:1,2,3,4
    console.log(attr2.toString())  //输出为:1,2,3,4,5,6
  • reverse方法

    var attr1 = [1,2,3,4];
    var attr2 = attr1.reverse();
    console.log(attr2)   //输出为:[4, 3, 2, 1]
  • sort方法

    var attr1 = [11,100,33,44];
    var attr2 = attr1.sort();
    console.log(attr2)  //输出为:[100, 11, 33, 44]
    ----------------------------------------------------------------------------
    //如需要常规方式排序,则需要编写一个判断大小的函数
    function f(a, b){
        if(a>b){
            return 1;
        }else if (a<b){
            return -1;
        }else{
            return 0;
        }
    }
    var attr1 = [11,100,33,44];
    var attr2 = attr1.sort(f);
    console.log(attr2)  //输出为:[11, 33, 44, 100]
    ----------------------------------------------------------------------------
    //由于传进sort的函数,是根据函数的返回值进行判断的,所以函数可简写为
    function f(a, b){ return a-b; }
  • 栈方法

    var attr = [1, 2, 3];
    attr.push(4, 5, [6, 7]);          //元素进栈
    console.log(attr)                 //输出为:[1, 2, 3, 4, 5, Array(2)]
    -------------------------------------------------------------------------
    attr.pop()                        //元素出栈
    console.log(attr)                 //输出为:[1, 2, 3, 4, 5]
    -------------------------------------------------------------------------
    attr.unshift("hello", "world")   //从首端进栈,注意顺序
    console.log(attr)                //输出为:["hello", "world", 1, 2, 3, 4, 5]
    -------------------------------------------------------------------------
    attr.shift()                     //从首端出栈
    console.log(attr)                //输出为:["world", 1, 2, 3, 4, 5]

 

三、Function对象

  •  函数的创建

    //最常用的方式
    function function_name1(param1, paramn){
        //...
        return null;
    }
    ----------------------------------------------------------------------------
     //一般不用的方式
    var function_name2 = new Function("param1","paramn","function_body")
  • 匿名函数

    //创建方式一
    var func = function (param) {
        return param;
    }
    ----------------------------------------------------------------------------
    //创建方式二
    (function (param) {
        console.log(param)
    })("有趣的创建方式");

    function fun(param1, param2) {
        console.log(param1 + param2)
    }

    //call
    fun(1, 2);      //输出:3
    fun(1, 2, 3);   //输出:3
    fun(1);         //输出:NaN
    fun();          //输出:NaN
    ----------------------------------------------------------------------------
    function a(a, b) {
        alert(a + b);
    }
    var a = 1;
    var b = 2;
    //call
    a(a, b)
    注:变量a将函数a覆盖,导致调用函数时找不到函数a而出错。
  • arguments的使用

    function nxAdd() {
        var result = 0;
        for (var num in arguments) {
            result += arguments[num]
        }
        alert(result)
    }
    nxAdd(1, 2, 3, 4, 5)
    -----------------------------------------------------------------------------------
    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!")
        }
    }
    //call
    f(1, 2, 3, 4, 5)
    -----------------------------------------------------------------------------------
    //arguments可以保存函数的所有参数

 

 

 

猜你喜欢

转载自www.cnblogs.com/Lynnblog/p/9147879.html
今日推荐