JS常用内置对象

内置对象

 

一、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可以保存函数的所有参数

 

 

 

 

转载于:https://www.cnblogs.com/Lynnblog/p/9147879.html

好的,下面是几个常用内置对象的使用例题: 1. 字符串对象 ```js let str = "hello world"; // 获取字符串长度 console.log(str.length); // 11 // 转换为大写字母 console.log(str.toUpperCase()); // "HELLO WORLD" // 替换字符串 console.log(str.replace("world", "JavaScript")); // "hello JavaScript" ``` 2. 数组对象 ```js let arr = [1, 2, 3, 4, 5]; // 获取数组长度 console.log(arr.length); // 5 // 获取数组中第一个元素 console.log(arr[0]); // 1 // 循环遍历数组 for (let i = 0; i < arr.length; i++) { console.log(arr[i]); } // 在数组末尾添加一个元素 arr.push(6); console.log(arr); // [1, 2, 3, 4, 5, 6] // 删除数组中最后一个元素 arr.pop(); console.log(arr); // [1, 2, 3, 4, 5] ``` 3. Date对象 ```js let now = new Date(); // 获取当前时间 console.log(now); // Sat Jul 10 2021 14:32:06 GMT+0800 (中国标准时间) // 获取当前时间的年份 console.log(now.getFullYear()); // 2021 // 获取当前时间的月份(0表示1月) console.log(now.getMonth()); // 6 // 获取当前时间的日期 console.log(now.getDate()); // 10 ``` 4. Math对象 ```js // 获取随机数(0-1之间) console.log(Math.random()); // 0.123456789 // 获取一个数的绝对值 console.log(Math.abs(-10)); // 10 // 获取两个数的最大值 console.log(Math.max(1, 2, 3, 4, 5)); // 5 ``` 以上只是一些常用内置对象的例题,JavaScript内置对象非常多,不同的对象有不同的属性和方法,需要根据实际需求灵活运用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值