数组全部函数
Array.prototype.map():对数组中的每个元素进行操作,并返回一个新数组。Array.prototype.filter():根据指定的条件筛选数组中的元素,并返回一个新数组。Array.prototype.reduce():将数组中的元素累积为单个值。Array.prototype.forEach():对数组中的每个元素执行指定的操作。Array.prototype.some():检查数组中是否至少有一个元素满足指定条件。Array.prototype.every():检查数组中的所有元素是否都满足指定条件。Array.prototype.find():返回数组中满足指定条件的第一个元素。Array.prototype.findIndex():返回数组中满足指定条件的第一个元素的索引。Array.prototype.includes():检查数组中是否包含指定元素。Array.prototype.sort():对数组中的元素进行排序。Array.prototype.reverse():颠倒数组中元素的顺序。Array.prototype.slice():提取数组中的一部分元素,返回一个新数组。Array.prototype.splice():在数组中插入、删除或替换元素。Array.prototype.concat():连接两个或多个数组,并返回一个新数组。Array.prototype.join():将数组中的所有元素连接成一个字符串。Array.prototype.push():向数组的末尾添加一个或多个元素,并返回新数组的长度。Array.prototype.pop():删除数组中的最后一个元素,并返回该元素的值。Array.prototype.shift():删除数组中的第一个元素,并返回该元素的值。Array.prototype.unshift():向数组的开头添加一个或多个元素,并返回新数组的长度。Array.prototype.indexOf():返回指定元素在数组中的第一个匹配项的索引。Array.prototype.lastIndexOf():返回指定元素在数组中的最后一个匹配项的索引。Array.prototype.flat():将多维数组扁平化为一维数组。Array.prototype.flatMap():将数组中的每个元素进行映射和扁平化操作,并返回一个新数组。Array.prototype.reduceRight():从数组的末尾开始,将数组中的元素累积为单个值。
对象全部函数
Object.keys():返回一个包含对象所有可枚举属性的数组。Object.values():返回一个包含对象所有可枚举属性的值的数组。Object.entries():返回一个包含对象所有可枚举属性的键值对数组。Object.assign():将一个或多个源对象的属性复制到目标对象。Object.hasOwnProperty():检查对象自身是否具有指定的属性。Object.freeze():冻结对象,使其属性不可修改。Object.getOwnPropertyNames():返回一个包含对象所有属性的数组,包括不可枚举属性。Object.getOwnPropertyDescriptor():返回指定属性的属性描述符对象。Object.seal():封闭对象,阻止添加新属性和删除现有属性,但允许修改属性的值。Object.isSealed():检查对象是否被封闭,即属性是否不可添加或删除。Object.getOwnPropertyDescriptors():返回一个包含对象所有属性的属性描述符对象的键值对。Object.getPrototypeOf():返回指定对象的原型。Object.setPrototypeOf():设置指定对象的原型。Object.is():比较两个值是否相同。Object.create():使用指定的原型和属性创建一个新对象。Object.getOwnPropertySymbols():返回一个包含对象所有符号属性的数组。Object.fromEntries():将一个包含键值对的数组转换为一个对象。Object.isExtensible():检查对象是否可扩展,即是否可以添加新属性。Object.preventExtensions():使对象不可扩展,阻止添加新属性。Object.isSealed():检查对象是否被封闭,即属性是否不可添加或删除。Object.isPrototypeOf():检查对象是否是另一个对象的原型。Object.getOwnPropertySymbols():返回一个包含对象所有符号属性的数组。Object.fromEntries():将一个包含键值对的数组转换为一个对象。Object.isExtensible():检查对象是否可扩展,即是否可以添加新属性。Object.preventExtensions():使对象不可扩展,阻止添加新属性。Object.isSealed():检查对象是否被封闭,即属性是否不可添加或删除。Object.isPrototypeOf():检查对象是否是另一个对象的原型。
数组函数案例
// map()
const numbers = [1, 2, 3, 4, 5];
const doubledNumbers = numbers.map((num) => num * 2);
console.log(doubledNumbers); // Output: [2, 4, 6, 8, 10]
// filter()
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter((num) => num % 2 === 0);
console.log(evenNumbers); // Output: [2, 4]
// reduce()
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((acc, num) => acc + num, 0);
console.log(sum); // Output: 15
// forEach()
const numbers = [1, 2, 3, 4, 5];
numbers.forEach((num) => console.log(num));
// Output:
// 1
// 2
// 3
// 4
// 5
// some()
const numbers = [1, 2, 3, 4, 5];
const hasEvenNumber = numbers.some((num) => num % 2 === 0);
console.log(hasEvenNumber); // Output: true
// every()
const numbers = [1, 2, 3, 4, 5];
const allEvenNumbers = numbers.every((num) => num % 2 === 0);
console.log(allEvenNumbers); // Output: false
// find()
const numbers = [1, 2, 3, 4, 5];
const firstEvenNumber = numbers.find((num) => num % 2 === 0);
console.log(firstEvenNumber); // Output: 2
// findIndex()
const numbers = [1, 2, 3, 4, 5];
const firstEvenNumberIndex = numbers.findIndex((num) => num % 2 === 0);
console.log(firstEvenNumberIndex); // Output: 1
// includes()
const numbers = [1, 2, 3, 4, 5];
const hasNumberThree = numbers.includes(3);
console.log(hasNumberThree); // Output: true
// sort()
const fruits = ['banana', 'apple', 'orange', 'grape'];
fruits.sort();
console.log(fruits); // Output: ['apple', 'banana', 'grape', 'orange']
// reverse()
const numbers = [1, 2, 3, 4, 5];
numbers.reverse();
console.log(numbers); // Output: [5, 4, 3, 2, 1]
// slice()
const numbers = [1, 2, 3, 4, 5];
const slicedNumbers = numbers.slice(1, 4);
console.log(slicedNumbers); // Output: [2, 3, 4]
// splice()
const numbers = [1, 2, 3, 4, 5];
numbers.splice(2, 1, 6);
console.log(numbers); // Output: [1, 2, 6, 4, 5]
// concat()
const numbers1 = [1, 2, 3];
const numbers2 = [4, 5, 6];
const combinedNumbers = numbers1.concat(numbers2);
console.log(combinedNumbers); // Output: [1, 2, 3, 4, 5, 6]
// join()
const fruits = ['apple', 'banana', 'orange'];
const joinedFruits = fruits.join(', ');
console.log(joinedFruits); // Output: "apple, banana, orange"
// push()
const numbers = [1, 2, 3];
const newLength = numbers.push(4, 5);
console.log(numbers); // Output: [1, 2, 3, 4, 5]
console.log(newLength); // Output: 5
// pop()
const numbers = [1, 2, 3];
const lastNumber = numbers.pop();
console.log(numbers); // Output: [1, 2]
console.log(lastNumber); // Output: 3
// shift()
const numbers = [1, 2, 3];
const firstNumber = numbers.shift();
console.log(numbers); // Output: [2, 3]
console.log(firstNumber); // Output: 1
// unshift()
const numbers = [2, 3];
const newLength = numbers.unshift(1);
console.log(numbers); // Output: [1, 2, 3]
console.log(newLength); // Output: 3
// indexOf()
const numbers = [1, 2, 3, 4, 5];
const index = numbers.indexOf(3);
console.log(index); // Output: 2
// lastIndexOf()
const numbers = [1, 2, 3, 4, 3, 5];
const lastIndex = numbers.lastIndexOf(3);
console.log(lastIndex); // Output: 4
// Array.prototype.flat()
const numbers = [1, [2, [3, [4]]]];
const flattenedNumbers = numbers.flat();
console.log(flattenedNumbers); // Output: [1, 2, 3, 4]
对象函数案例
// Object.keys()
const obj = { a: 1, b: 2, c: 3 };
const keys = Object.keys(obj);
console.log(keys); // 输出:['a', 'b', 'c']
// Object.values()
const obj = { a: 1, b: 2, c: 3 };
const values = Object.values(obj);
console.log(values); // 输出:[1, 2, 3]
// Object.entries()
const obj = { a: 1, b: 2, c: 3 };
const entries = Object.entries(obj);
console.log(entries); // 输出:[['a', 1], ['b', 2], ['c', 3]]
// Object.assign()
const target = { a: 1 };
const source = { b: 2 };
const result = Object.assign(target, source);
console.log(result); // 输出:{ a: 1, b: 2 }
// Object.hasOwnProperty()
const obj = { a: 1, b: 2 };
console.log(obj.hasOwnProperty('a')); // 输出:true
console.log(obj.hasOwnProperty('c')); // 输出:false
// Object.freeze()
const obj = { a: 1, b: 2 };
Object.freeze(obj);
obj.a = 3; // 尝试修改属性,但不会生效
console.log(obj); // 输出:{ a: 1, b: 2 }
// Object.getOwnPropertyNames()
const obj = { a: 1, b: 2 };
const propertyNames = Object.getOwnPropertyNames(obj);
console.log(propertyNames); // 输出:['a', 'b']
// Object.getOwnPropertyDescriptor()
const obj = { a: 1, b: 2 };
const descriptor = Object.getOwnPropertyDescriptor(obj, 'a');
console.log(descriptor); // 输出:{ value: 1, writable: true, enumerable: true, configurable: true }
// Object.seal()
const obj = { a: 1, b: 2 };
Object.seal(obj);
obj.c = 3; // 尝试添加新属性,但不会生效
delete obj.a; // 尝试删除属性,但不会生效
obj.b = 4; // 修改属性的值,会生效
console.log(obj); // 输出:{ a: 1, b: 4 }
// Object.isSealed()
const obj = { a: 1, b: 2 };
console.log(Object.isSealed(obj)); // 输出:false
Object.seal(obj);
console.log(Object.isSealed(obj)); // 输出:true
// Object.getOwnPropertyDescriptors()
const obj = { a: 1, b: 2 };
const descriptors = Object.getOwnPropertyDescriptors(obj);
console.log(descriptors); // 输出:{ a: { value: 1, writable: true, enumerable: true, configurable: true }, b: { value: 2, writable: true, enumerable: true, configurable: true } }
// Object.getPrototypeOf()
const obj = {};
const prototype = Object.getPrototypeOf(obj);
console.log(prototype); // 输出:{}
// Object.setPrototypeOf()
const obj = {};
const prototype = { a: 1 };
Object.setPrototypeOf(obj, prototype);
console.log(obj.a); // 输出:1
// Object.is()
console.log(Object.is(1, 1)); // 输出:true
console.log(Object.is(1, '1')); // 输出:false
// Object.create()
const prototype = { a: 1 };
const obj = Object.create(prototype);
console.log(obj.a); // 输出:1
// Object.getOwnPropertySymbols()
const sym = Symbol('symbol');
const obj = { [sym]: 'value' };
const symbols = Object.getOwnPropertySymbols(obj);
console.log(symbols); // 输出:[Symbol(symbol)]
// Object.fromEntries()
const entries = [['a', 1], ['b', 2]];
const obj = Object.fromEntries(entries);
console.log(obj); // 输出:{ a: 1, b: 2 }
// Object.isExtensible()
const obj = { a: 1 };
console.log(Object.isExtensible(obj)); // 输出:true
Object.preventExtensions(obj);
console.log(Object.isExtensible(obj)); // 输出:false
// Object.preventExtensions()
const obj = { a: 1 };
Object.preventExtensions(obj);
obj.b = 2; // 尝试添加新属性,但不会生效
console.log(obj); // 输出:{ a: 1 }
// Object.isSealed()
const obj = { a: 1 };
console.log(Object.isSealed(obj)); // 输出:false
Object.seal(obj);
console.log(Object.isSealed(obj)); // 输出:true
// Object.isPrototypeOf()
const prototype = { a: 1 };
const obj = Object.create(prototype);
console.log(prototype.isPrototypeOf(obj)); // 输出:true