js内置对象的用法

199 阅读2分钟

简述:JS内置对象即为Javascript自带的对象,并且提供了一些常用的的功能。

四种内置对象:

1.Array对象:提供一个数组的模型来存储大量有序的数据。

2.Math对象:可以处理所有的数学运算 。

3.Date对象:可以处理日期和时间的存储、转化和表达。

4.String对象:可以处理所有的字符串的操作。

一、string 字符串

image.png

image.png

"abcdefg".charAt(1)  // 参数: n 任意的正整数  返回值: n下标的对应字符值,n<0 n>string.length  返回空串
 
 
"abcdef".charCodeAt(n) // 返回的是n下标对应字符的Unicode编码
拼接字符串: (1) +   (2)string.concat(value,.....)  
   var a = 'abc'
   var b = 'cdefg'
   var c = 'oooppp i have a apple'
   /*  document.write(a+b+c)*/
    document.write(a.concat(b,c))
 
###indexof(substring 子串,start )
 console.log(b.indexOf(c))  如果不存在这个子串返回-1 省略第二个参数start默认索引为0下标
 
 替换: 父串.replace(正则,子串)
 
 
text = "javascripthelloJavascript"
text.replace(/javascript/i, "JavaScript"); ===>"JavascripthelloJavascript"
### string.slice(start, end)  提取子串
var  str = "abgdh234lkdgjrf"
str.slice(2,5) 
### split(分隔符)  字符串---》数组
"2023-2-30".split('-')===>['2023','2','30']
url : 
https://home.firefoxchina.cn/?name='lili'&year=23
var str =" https://home.firefoxchina.cn/?name='lili'&year=23"
undefined
str.indexOf('?')
30
str.slice(31)
"name='lili'&year=23"
var str1 = "name='lili'&year=23"
undefined
str1.split('&')
(2) ["name='lili'", "year=23"]

二、数组方法(Array)

image.png


###join('连接符')  数组元素+连接符---》字符串
 
a = new Array(1, 2, 3, "testing");
 
s = a.join(" + ");  // s 是字符串"1+2+3+testing"
栈结构方法:
push()   将数据入到数组尾部  str.push(1)  str = [a,b,c]  ===>[a,b,c,1]
## 返回值 : 数组的总长度
pop()    str.pop() ===>[a,b,c] 弹出数组尾部的一个元素
## 返回值 :弹出的数据 1
 
队列:
shift()  出数组的第一个元素 ##返回值 是该元素
unshift()  插入一个数据到数组的头部  ## 返回值: 数组的新长度
 
 

三、时间对象 Data

image.png

    <script>
        /*获取时间*/
        var date = new Date();
        console.log(date);//输出时间格式:Tue Nov 16 2021 10:11:14 GMT+0800 (中国标准时间)
        //对输出时间的默认格式进行转化的方法
        console.log(date.toDateString());
        console.log(date.toISOString());
        console.log(date.toLocaleDateString());
        console.log(date.toLocaleString());
        console.log(date.toTimeString());
        console.log(date.toUTCString());
        //获取具体时间的方法
        console.log(date.getDate());//日
        console.log(date.getDay());//周
        console.log(date.getFullYear());//年
        console.log(date.getYear());//当前两份到1900年总年数
        console.log(date.getMonth());//月(月份默认减掉了一,因为从0-11)
        console.log(date.getHours());//小时
        console.log(date.getMinutes());//分钟
        console.log(date.getSeconds());//秒
        console.log(date.getMilliseconds());//毫秒
        console.log(date.getTime());//返回当前时间到1970年1月1日的总毫秒数

        /*统一设置时间*/
        var time = new Date("2020/12/29 00:00:00");
        var time = new Date("2020-12-28 00:00:00");//字符串的设置方法比较方便,因为不涉及月份的加一减一
        var time = new Date(2020, 10, 28, 0, 0, 0);

        /*分开设置时间*/
        var time =new Date();
        time.setDate(28);//设置日
        time.setFullYear(2021);//设置年
        time.setMonth(1);//设置月,实际出来加一之后为2月
        time.setHours(9);//设置时
        time.setMinutes(0);//设置分钟
        time.setSeconds(0);//设置秒
        time.setMilliseconds(1000);//设置毫秒
        console.log(time);
    </script>
</body>
</html>

表达式1 ?表达式2;表达式3

表达式1成立,则执行表达式2,不成立则执行表达式3---》if else

2021-09-28   08:57  if(new Date().getHour()<10?new Date().getHour() : '0'+new Date().getHour())

显示当前时间:

    var now = new Date()
    var year = now.getFullYear()
    var month = now.getMonth()
    var date = now.getDate()
    var hours = now.getHours()
    var min = now.getMinutes()
    var sec = now.getSeconds()
    var wee = now.getUTCDay()
    document.write(year+'-'+(month+1)+'-'+date+'   '+hours+':'+min+':'+sec+'  '+'星期'+wee)

四、Math对象

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <script>
        console.log(Math);
        console.log(Math.PI);//输出为π
        console.log(Math.floor(1.9));//向下取整
        console.log(Math.ceil(1.1));//向上取整
        console.log(Math.round(4.3));//四舍五入
        console.log(Math.abs(-33));//取绝对值
        console.log(Math.max(1, 2, 3, 4, 5));//取最大值
        console.log(Math.max.apply(null, [1, 2, 3, 4, 5]));//null表示this指针不替换
        console.log(Math.min(1, 2, 3, 4, 5));//取最小值
        console.log(Math.min.apply(null, [1, 2, 3, 4, 5]));
        console.log(Math.sqrt(100));//开平方
        console.log(Math.pow(5, 3));//幂,5的3次方
        console.log(Math.sin(90 * Math.PI / 180));//正弦,弧度=角度*π/180
        console.log(Math.cos(180 * Math.PI / 180));//余弦
        console.log(Math.tan(90 * Math.PI / 180));//正切
    </script>
</body>
</html>