JavaScript的内置对象

一、Math

Math对象:是一个内置对象,用于数学运算的。不是一个函数对象(通过构造函数创建的对象称为函数对象),在创建Math对象不能使用new运算符(即不能 new Math()),它的属性和方法在使用时采用:

Math.属性名

Math.方法名(【参数】)

(1)属性:

Math.PI :表示圆周率

(2)方法:

Math.abs(x) :返回参数x的绝对值

Math.floor(x):返回小于等于形参x的最大整数。向下取整

Math.ceil(x):返回大于等于形参x的最小整数。向上取整

Math.max(x,y,z...):返回所有参数的最大值

Math.min(x,y,z...):返回所有参数的最小值

Math.pow(x,y):返回x的y次方

Math.sqrt(x):返回x的算术平方根

Math.round(x):取整,x四舍五入后的整数

Math.random():返回0.0~1.0之间的随机数(不包含1.0)

Math.trunc():会将数字的小数部分去掉,只保留整数部分,传入该方法的参数会被隐式转换成数字 类型

//1
        //π 的值
        console.log(Math.PI);

        //2
        //abs:绝对值
        let k =Math.abs(-10);
        console.log(k);//10

        //3
        //floor(向下取整):返回小于等于形参的最大整数
        let m = 3.99;
        console.log('floor:',Math.floor(m));//3

        //4
        //max:获取所有参数中的最大值
        console.log('max:',Math.max(12,15,98,76));//98
        let arr = [12,25,98,36,47];
        console.log('max:',Math.max(...arr));//98

        //5
        //min:返回所有参数的最小值
        console.log('min:',Math.min(12,15,98,76));//12
        let a = [12,25,98,36,47];
        console.log('min:',Math.min(...arr));//12

        //6
        //ceil:(向上取整):返回大于等于形参的最小整数
        let x = 3.99;
        console.log('ceil:',Math.ceil(x));//4
        let y = 4.01;
        console.log('ceil:',Math.ceil(y));//5
        let z = 9;
        console.log('ceil:',Math.ceil(z));//9

        //7
        //pow:返回x的y次方
        console.log('pow:',Math.pow(2,3));//8

        //8
        //sqrt:x的算术平方根
        console.log('sqrt:',Math.sqrt(2));//1.414

        //9
        //round取整:四舍五入后的整数
        console.log('round:',Math.round(6.52));//7
        console.log('round:',Math.round(6.02));//6

        //10
        //random:返回0.0——1.0之间的随机数(不包括1.0)
        console.log('random:',Math.random());// 0.5614161018425583

		//11
 		console.log(Math.trunc(13.37))    // 13
        console.log(Math.trunc(42.84))   // 42
        console.log(Math.trunc(0.123))    //  0
        console.log(Math.trunc(-0.123))   // -0
        console.log(Math.trunc("-1.123")) // -1
        console.log(Math.trunc(NaN))      // NaN
        console.log(Math.trunc("foo"))    // NaN
        console.log(Math.trunc())         // NaN

二、Date是一个函数对象,使用new 运算符创建对象

(1)构造函数:

a、无参的构造函数:new Date() --->使用的日期格式是月日年

b、传入年月日、时分秒:new Date(年,月,日,时,分,秒) --->月份值应该在0~11之间,0表示1月,11表示12月

c、传入字符串表示日期和时间:new Date(‘字符串’)

d、传入整数:new Date(整数) --->整数表示毫秒数

	let d1 = new Date();//无参
        console.log(d1);//Fri Oct 21 2022 15:40:15 GMT+0800 (中国标准时间)

        let d2 = new Date(2022,9,21,15,42,50)//传入年月日时分秒
        console.log(d2);//Mon Nov 21 2022 15:42:50 GMT+0800 (中国标准时间)

        let d3 = new Date('2022-10-21 15:46:45');
        console.log(d3);//Mon Nov 21 2022 15:42:50 GMT+0800 (中国标准时间)

        let d4 = new Date('2022-9-9 15:46:45');
        console.log(d4);//Fri Sep 09 2022 15:46:45 GMT+0800 (中国标准时间)

        let d5 = new Date('2022-9-9');
        console.log(d5);//Fri Sep 09 2022 00:00:00 GMT+0800 (中国标准时间)

        let d6 = new Date(1666339352280);
        console.log(d6);//Fri Oct 21 2022 16:02:32 GMT+0800 (中国标准时间)

(2)其他函数:

a、getFullYear():获取年份(4位)

b、getMonth():获取月份(0~11)

c、getDate():获取日期(月份中的某一天,1~31)

d、getDay():获取星期(0~6,0表示星期天)

e、getHours():获取小时数

f、getMinutes():获取分钟数

g、getTime():获取1970年1月1日0时0分0秒到当前日期时间之间的毫秒数

h、toLocaleDateString():返回该日期对象日期部分的字符串(即将年月日转换为字符串)

i、toLocaleString():返回该日期对象的字符串(即将日期对象转换为字符串)

j、toLocaleTimeString():返回该日期对象时间部分的字符串(将时分秒转换为字符串)

k、toTimeString():以人类易读形式返回一个日期对象时间部分的字符串

let d1 = new Date();
        console.log(d1);//Fri Oct 21 2022 16:21:45 GMT+0800 (中国标准时间)

        console.log('年份:',d1.getYear());//年份: 122----->用现在年份2022减去1900得到的值,该函数已经废弃

        console.log('年:',d1.getFullYear());//年: 2022
        console.log('月:',d1.getMonth() + 1);//月: 10
        console.log('日:',d1.getDate());//日: 21
        console.log('星期:',d1.getDay());//星期: 5

        console.log('时:',d1.getHours());//时: 16
        console.log('分:',d1.getMinutes());//分: 23
        console.log('秒:',d1.getSeconds());//秒: 12
        console.log('毫秒:',d1.getMilliseconds());//毫秒: 146

        console.log('时间戳:',d1.getTime());//时间戳: 1666340592146

        console.log(d1.toLocaleDateString());//2022/10/21
        console.log(d1.toLocaleString());//2022/10/21 16:54:45
        console.log(d1.toLocaleTimeString());//16:54:45
        console.log(d1.toTimeString());//16:56:30 GMT+0800 (中国标准时间)

三、String对象:字符串。用单引号(’’) 或双引号(””)括起来的字符序列

1、创建方式:

a、字面量:'' 或 ""

b、使用构造函数:new String()

(2)字符

2、String对象的常用方法:字符串名.方法名(【实参】)

(1)charAt( index ):返回字符串中index位置上的字符。参数index代表索引值(下标),若没有找到则返回空

(2)charCodeAt(index):返回字符串中index位置上的字符的Unicode编码

ASCII码:是美国做的字符编码 (’a’:97…..) ——— 只支持英文字符。表示一个字符使用1个字节(byte),即8个二进制位(bit)

Unicode码:国际标准化组织做的一套编码。表示一个字符使用2个字节,即16个二进制位(bit) —— 支持中文

ISO-8859-1码:不支持中文

GBK:支持中文

GB2312:支持简体中文

(3)concat(字符串):将两个及两个以上的字符串进行连接

(4)endsWith(字符串):判断字符串是否是以给定的子串结尾。是则返回true,不是则返回false

(5)indexOf(子串):返回子串在字符串中首次出现的位置(下标)。若返回-1,表示没有找到子串

(6)lastIndexOf(子串):返回子串在字符串中最后出现的位置(下标)

(7)includes(子串):查找字符串中是否包含指定的子串。若包含返回true,不包含返回false

(8)startsWith(子串):判断字符串是否以给定子串开头。是则返回true,不是则返回false

(9)split(分隔字符):将字符串分割成字符串数组

(10)replace(oldStr,newStr):在字符中查找oldStr第一次出现的位置,并用newStr替换它

(11)substr(start,length):截取字符串中从start开始的连续length个字符。当参数length省略时,截取从start开始到串末尾的所有字符

(12)substring(start,end):截取start到end之间的子串,不包含end

(13)trim():去掉字符串两端的空白字符

(14)trimEnd():去掉字符串末尾的空白字符

(15)trimStart():去掉字符串前面的空白字符

(16)toLowerCase():将字符串中的所有字母转换为小写字母

(17)toUpperCase():将字符串中的所有字母转换为大写字母

(18)toString():将字符串对象转换成字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值