返回

js常用内置对象及方法解析及应用大全

前端

JavaScript 常用内置对象及方法详解

JavaScript 是一门广泛应用于 Web 开发和服务器端编程的编程语言。它具有丰富的内置对象和方法,可用于执行各种任务,例如创建和操作数组、执行数学运算、处理字符串和日期等。本文将介绍 JavaScript 中最常用的内置对象和方法,帮助您更深入地理解和使用 JavaScript 语言。

一、Array 数组对象

数组是 JavaScript 中一种有序的集合,可以存储各种数据类型。Array 对象提供了一系列方法来操作数组中的元素,包括添加、删除、排序和搜索等。

1. push() 方法

push() 方法用于在数组的尾部添加一个或多个元素,并返回数组的新长度。

const arr = [1, 2, 3];
arr.push(4, 5);
console.log(arr); // 输出:[1, 2, 3, 4, 5]

2. pop() 方法

pop() 方法用于从数组的尾部删除一个元素,并返回该元素。

const arr = [1, 2, 3];
const lastElement = arr.pop();
console.log(lastElement); // 输出:3
console.log(arr); // 输出:[1, 2]

3. shift() 方法

shift() 方法用于从数组的头部删除一个元素,并返回该元素。

const arr = [1, 2, 3];
const firstElement = arr.shift();
console.log(firstElement); // 输出:1
console.log(arr); // 输出:[2, 3]

4. unshift() 方法

unshift() 方法用于在数组的头部添加一个或多个元素,并返回数组的新长度。

const arr = [1, 2, 3];
arr.unshift(4, 5);
console.log(arr); // 输出:[4, 5, 1, 2, 3]

5. sort() 方法

sort() 方法用于对数组中的元素进行排序。默认情况下,sort() 方法会将数组中的元素按升序排列。如果需要按降序排列,可以传入一个比较函数。

const arr = [3, 1, 2];
arr.sort();
console.log(arr); // 输出:[1, 2, 3]

arr.sort((a, b) => b - a);
console.log(arr); // 输出:[3, 2, 1]

6. reverse() 方法

reverse() 方法用于反转数组中的元素。

const arr = [1, 2, 3];
arr.reverse();
console.log(arr); // 输出:[3, 2, 1]

7. join() 方法

join() 方法用于将数组中的元素连接成一个字符串。默认情况下,join() 方法使用逗号(,)作为连接符。如果需要使用其他连接符,可以传入一个字符串作为参数。

const arr = [1, 2, 3];
const str = arr.join();
console.log(str); // 输出:"1,2,3"

const str = arr.join(' ');
console.log(str); // 输出:"1 2 3"

8. slice() 方法

slice() 方法用于从数组中截取一部分元素并返回一个新的数组。slice() 方法接受两个参数:开始索引和结束索引。开始索引表示要截取的第一个元素的索引,结束索引表示要截取的最后一个元素的索引(不包括)。

const arr = [1, 2, 3, 4, 5];
const newArr = arr.slice(1, 3);
console.log(newArr); // 输出:[2, 3]

9. splice() 方法

splice() 方法用于从数组中添加、删除或替换元素。splice() 方法接受三个参数:开始索引、删除元素的个数和要添加的元素。

const arr = [1, 2, 3, 4, 5];
arr.splice(2, 2, 6, 7);
console.log(arr); // 输出:[1, 2, 6, 7, 5]

10. concat() 方法

concat() 方法用于将两个或多个数组合并成一个新的数组。

const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const newArr = arr1.concat(arr2);
console.log(newArr); // 输出:[1, 2, 3, 4, 5, 6]

11. indexOf() 方法

indexOf() 方法用于查找数组中第一个与指定元素相等的元素的索引。如果数组中不包含该元素,则返回 -1。

const arr = [1, 2, 3, 4, 5];
const index = arr.indexOf(3);
console.log(index); // 输出:2

12. lastIndexOf() 方法

lastIndexOf() 方法用于查找数组中最后一个与指定元素相等的元素的索引。如果数组中不包含该元素,则返回 -1。

const arr = [1, 2, 3, 4, 5, 3];
const index = arr.lastIndexOf(3);
console.log(index); // 输出:5

13. includes() 方法

includes() 方法用于判断数组中是否包含指定元素。如果包含,则返回 true;否则,返回 false

const arr = [1, 2, 3, 4, 5];
const result = arr.includes(3);
console.log(result); // 输出:true

二、Math 方法

Math 对象提供了许多用于执行数学运算的方法,例如求绝对值、取整、四舍五入和生成随机数等。

1. Math.abs() 方法

Math.abs() 方法用于求一个数字的绝对值。

const number = -5;
const absoluteValue = Math.abs(number);
console.log(absoluteValue); // 输出:5

2. Math.ceil() 方法

Math.ceil() 方法用于对一个数字向上取整。

const number = 4.2;
const roundedUpNumber = Math.ceil(number);
console.log(roundedUpNumber); // 输出:5

3. Math.floor() 方法

Math.floor() 方法用于对一个数字向下取整。

const number = 4.8;
const roundedDownNumber = Math.floor(number);
console.log(roundedDownNumber); // 输出:4

4. Math.round() 方法

Math.round() 方法用于对一个数字进行四舍五入。

const number = 4.5;
const roundedNumber = Math.round(number);
console.log(roundedNumber); // 输出:5

5. Math.random() 方法

Math.random() 方法用于生成一个介于 0 和 1 之间的随机数。

const randomNumber = Math.random();
console.log(randomNumber); // 输出:0.23456789

三、String 字符串对象

字符串对象用于表示一组字符。String 对象提供了许多用于操作字符串的方法,例如获取字符串的长度、查找子字符串、替换子字符串等。

1. String.length 属性

String.length 属性用于获取字符串的长度。

const str = "Hello World";
const length = str.length;
console.log(length); // 输出: