内置对象

113 阅读7分钟

01_Number

属性:

Number.MAX_VALUE:JS中可表示的最大数字(了解)

console.log("JS中可以表示的最大的数字",Number.MAX_VALUE)
// JS中可以表示的最大的数字 1.7976931348623157e+308

Number.MIN_VALUE:JS中可表示的最小的正数(了解)

console.log("JS中可以表示的最小的数字",Number.MIN_VALUE)
// JS中可以表示的最小的数字 5e-324

方法:

toFixed()(熟悉)

let num = 12.45;
console.log(num.toFixed(1)); // 12.4

:::info toFixed():
参数:可以选择保留几位小数,默认不写是提取整数
返回值:要保留的数字,属于string类型 :::

toString()(熟悉)

let num = 12.45;
console.log(num.toString()); // 12.45

:::info toString():
参数:选择转换成几进制,可以选择从2进制到36进制
返回值:想要转换成的进制表达方法 :::

02_String

属性:

length

:::info string类型数据也有length属性,可以通过这个属性得到他对应的下标,可以进行for遍历,但不能修改其中的值,string是一个伪数组 :::

方法:

charAt()(了解)

let str = "hello world"
console.log(str.charAt(0)) // h
console.log(str.charAt(6)) // w
console.log(str[0]);  // h
// 也可以通过 [] 来指定索引

:::info charAt():
参数:可以指定索引
返回值:通过指定的索引返回对应的字符 :::

indexOf()(了解)

let str = "hello world"
console.log(str.indexOf("o"));  // 4
console.log(str.indexOf("1"));  // -1

:::info indexOf():
意义:查询字符在此字符串中第一次出现的位置
参数:需要查询的字符
返回值:如果有查询的字符,返回该字符的索引,如果有多个该字符,返回第一个字符的索引
如果没有查询的字符,则返回 -1 :::

lastIndexOf()

let str = "hello world"
console.log(str.lastIndexOf("o")) // 7
console.log(str.lastIndexOf("1")) // -1

:::info lastIndexOf():
意义:查询字符在此字符串中最后一次出现的位置
参数:需要查询的字符
返回值:如果有查询的字符,返回该字符的索引,如果有多个该字符,返回第一个字符的索引
如果没有查询的字符,则返回 -1 :::

slice()(精通)

let str = "hello world"
console.log(str.slice(0,2)); // he
console.log(str.slice(2,));  // llo world

:::info slice():
意义:字符串截取,可以指定从一串字符当中指定位置截取到指定位置结束并返回
参数:
参数1: 代表从哪开始截取
参数2:代表从这里结束,结束位置的索引并不会被截取
返回值:有,返回从开始截取位置到结束截取位置并不包括结束截取位置 :::

substr()(了解)

let str = "hello world"
console.log(str.substr(6, 4)); // worl
console.log(str.substr(6)); // world

:::info substr():
参数:
参数1:截取字符串开始索引位置
参数2:指定截取长度,如果此参数不填写,会截取到最后
返回值:有,返回从开始截取位置到指定截取长度的位置结束 :::

split()(精通)

let str = "hello world"
console.log(str.split("l")); // ['he', '', 'o wor', 'd']
console.log(str.split("e")); // ['h', 'llo world']
console.log(str.split(""));  // ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
console.log(str.split(" ")); // ['hello', 'world']

注意:如果参数重什么都不写,会把字符串全部分隔开

如:// ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']

空格可以当作分隔符,是有区别的

如: (""):['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] (" "):['hello', 'world']

:::info split():
意义:把字符串分割成数组
参数:指定分隔符,需要加“”,
返回值:返回一个数组,里面的属性是通过分隔符分开的属性 :::

toUpperCase()(了解)

let str = "hello world"
console.log(str.toUpperCase()) // HELLO WORLD

:::info toUpperCase():
意义:把字符串全部转为大写
参数:无
返回值:转换后的大写字符串 :::

lastLowerCase()(了解)

console.log("HELLO WORLD".toLowerCase()) // hello world

:::info lastLowerCase():
意义:把字符串全部转为小写
参数:无
返回值:转换后的小写字符串 :::

charCodeAt()(了解)

let str = "hello world"
console.log(str.charCodeAt(8)) // 114(number类型)

:::info charCodeAt()
意义:可以得到指定字符的unicode字符码
参数:指定位置的索引
返回值:有,指定位置索引的返回值 :::

replace():

let a = ",,qwer";
a.replace(",,","");
console.log(a); // qwer

构造函数:

String.fromCharCode()(了解)

console.log(String.fromCharCode(65)); // A
console.log(String.fromCharCode(97)); // a

:::info String.fromCharCode():
意义:根据unicode编码得到指定的字符
参数:unicode编码
返回值:unicode编码对应的字符 :::

03_Math

属性:

Math.PI(了解)

console.log(Math.PI) // 3.141592653589793

:::info Math.PI:
意义:返回圆周率 :::

方法:

Math.abs()(了解)

console.log(Math.abs(-1234)); // 1234
console.log(Math.abs(-12.34)) // 12.34

:::info Math.abs():
意义:绝对值
参数:需要求的值
返回值:有,返回参数的绝对值 :::

Math.sqrt()(了解)

console.log(Math.sqrt(4)); // 2
console.log(Math.sqrt(16)); // 4

:::info Math.sqrt():
意义:平方根
参数:需要求的值
返回值:有,返回参数的平方根 :::

Math.pow()(了解)

console.log(Math.pow(2,3)) //8
 console.log(Math.pow(3,2)) //6

:::info Math.pow():
意义:次方
参数:
参数1:要求的值
参数2:次方数
返回值:有,返回参数1的次方 :::

Math.floor()(精通)

console.log(Math.floor(123.45)); // 123
console.log(Math.floor(123.99)); // 123

:::info Math.floor():
意义:向下取整
参数:取整的值
返回值:有,返回去掉小数部分的整数 :::

Math.ceil()(精通)

console.log(Math.ceil(123.45)); // 124
console.log(Math.ceil(123.01)); // 124

:::info Math.ceil():
意义:向上取整
参数:取整的值
返回值:有,如果没有小数直接返回该值
如果有小数,直接舍去,整数加1 :::

Math.round()(了解)

console.log(Math.round(123.45)); // 123
console.log(Math.round(123.54)); // 124

:::info Math.round():
意义:四舍五入取整
参数:取整的值
返回值:有,如果没有小数直接返回该值
如果有小数,进行四舍五入计算,并返回 :::

Math.max()(精通)

console.log(Math.max(1,2,3,4,5,6,7,8,9,10));  // 10
console.log(Math.max(12,43,56,73,2,7,4,32,67,7,8));  // 73

:::info Math.max():
意义:取出参数中最大的值
参数:多个数字,string类型可以自动转换
返回值:有,返回参数中最大的值 :::

Math.min()(精通)

console.log(Math.min(1,2,3,4,5,6,7,8,9,10));  // 1
console.log(Math.min(12,43,56,73,2,7,4,32,67,7,8));  // 2

:::info Math.min():
意义:取出参数中最小的值
参数:多个数字,string类型可以自动转换
返回值:有,返回参数中最小的值 :::

Math.random()(精通)

console.log(Math.random());

:::info Math.random():
意义:随机数
参数:无
返回值:有,随机得到0~1之间的随机数 :::

04_Date

:::info Date 是一个构造函数,每次使用需要加 new 关键字 new Date();
想要使用Date身上的函数,通过实例化对象来调用 :::

let time = new Date();

:::info ↑此上代码可以得到目前的时间 :::

方法:

Date.now()

console.log(Date.now());

:::info Date.now():
意义:得到从1970-1-1:00:00:00到现在的毫秒数
返回值:有,返回一个时间戳,是1970-1-1:00:00:00到现在的毫秒数 :::

getFullYear()�:

var d1 = new Date();
console.log('年:', d1.getFullYear());

:::info getFullYear()�:
意义:得到指定日期的年份 :::

getMonth()��:

var d1 = new Date();
console.log('月:', d1.getMonth() + 1);

:::info getMonth():
意义:得到指定日期的月份 :::

注意:中国的月份需要加1,因为月份是从0还是计算的,此个特殊

getDay():

var d1 = new Date();
console.log('星期几:', d1.getDay());

:::info getDay():
意义:得到指定日期的星期几 :::

getDate()�:

var d1 = new Date();
console.log('年:', d1.getDate());

:::info getDate()��:
意义:得到指定日期的几号 :::

getHours():

var d1 = new Date();
console.log('时:', d1.getHours());

:::info getHours()�:
意义:得到指定日期的小时 :::

getMinutes():

var d1 = new Date();
console.log('分:', d1.getMinutes());

:::info getMinutes():
意义:得到指定日期的分钟 :::

getSeconds():

var d1 = new Date();
console.log('秒:', d1.getSeconds());

:::info getFullYear()�:
意义:得到指定日期的秒数 :::

getMilliseconds():

var d1 = new Date();
console.log('毫妙:', d1.getMilliseconds());

:::info getMilliseconds():
意义:得到指定日期的毫秒数 :::

05_Array

属性:

方法:

concat�():

let arr1 = [1,2,3,4,5];
let arr2 = [6,7,8,9,0];
let arr3 = arr1.concat(arr2);
console.log(arr1) // [1, 2, 3, 4, 5]
console.log(arr2) // [6, 7, 8, 9, 0]
console.log(arr3) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

:::info concat():
意义:返回一个由当前数组和其他若个数组组成的新数组
参数:组合的数组
返回值:有,返回一个新数组,新数组成员由调用者的属性和参数数组里的属性组合而成 :::

slice():

let arr1 = [1,2,3,4,5];
 console.log(arr1.slice(0,2)); // [1, 2]

:::info slice():
意义:截取字符串中的一部分,返回一个新数组
参数:
参数1:开始截取数组中
返回值:有,返回一个新数组,新数组成员由调用者的属性和参数数组里的属性组合而成 :::

join():

let arr1 = [1, 2, 3, 4, 5];
console.log(arr1.join()); // 1,2,3,4,5
console.log(arr1.join('-')); // 1-2-3-4-5
console.log(arr1.join('<->')); // 1<->2<->3<->4<->5
console.log(arr1.join('')); // 12345

:::info join():
意义:连接数组中所有的元素组成一个字符串
参数:连接符
返回值:有,返回组成的字符串 :::

join() 与 字符串的 split() 方法互为逆运算�