您好,登录后才能下订单哦!
ES6(ECMAScript 2015)引入了许多新的内置对象扩展方法,这些方法极大地增强了JavaScript的功能性和易用性。本文将介绍一些常用的ES6内置对象扩展方法,并通过示例展示如何使用它们。
Object.assign()
Object.assign()
方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。
Object.assign(target, ...sources)
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);
console.log(target); // { a: 1, b: 4, c: 5 }
console.log(returnedTarget); // { a: 1, b: 4, c: 5 }
Array.prototype.includes()
Array.prototype.includes()
方法用于判断数组是否包含某个特定的元素,返回 true
或 false
。
array.includes(searchElement, fromIndex)
const array = [1, 2, 3];
console.log(array.includes(2)); // true
console.log(array.includes(4)); // false
String.prototype.startsWith()
String.prototype.startsWith()
方法用于判断字符串是否以指定的子字符串开头,返回 true
或 false
。
str.startsWith(searchString, position)
const str = 'Hello, world!';
console.log(str.startsWith('Hello')); // true
console.log(str.startsWith('world', 7)); // true
String.prototype.endsWith()
String.prototype.endsWith()
方法用于判断字符串是否以指定的子字符串结尾,返回 true
或 false
。
str.endsWith(searchString, length)
const str = 'Hello, world!';
console.log(str.endsWith('world!')); // true
console.log(str.endsWith('Hello', 5)); // true
String.prototype.includes()
String.prototype.includes()
方法用于判断字符串是否包含指定的子字符串,返回 true
或 false
。
str.includes(searchString, position)
const str = 'Hello, world!';
console.log(str.includes('world')); // true
console.log(str.includes('goodbye')); // false
Array.prototype.find()
Array.prototype.find()
方法用于返回数组中满足提供的测试函数的第一个元素的值。如果没有找到,则返回 undefined
。
array.find(callback(element, index, array), thisArg)
const array = [5, 12, 8, 130, 44];
const found = array.find(element => element > 10);
console.log(found); // 12
Array.prototype.findIndex()
Array.prototype.findIndex()
方法用于返回数组中满足提供的测试函数的第一个元素的索引。如果没有找到,则返回 -1
。
array.findIndex(callback(element, index, array), thisArg)
const array = [5, 12, 8, 130, 44];
const foundIndex = array.findIndex(element => element > 10);
console.log(foundIndex); // 1
Array.prototype.fill()
Array.prototype.fill()
方法用于用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。
array.fill(value, start, end)
const array = [1, 2, 3, 4];
console.log(array.fill(0, 2, 4)); // [1, 2, 0, 0]
console.log(array.fill(5, 1)); // [1, 5, 5, 5]
console.log(array.fill(6)); // [6, 6, 6, 6]
Array.prototype.flat()
Array.prototype.flat()
方法用于将嵌套的数组“拉平”,变成一维的数组。该方法返回一个新数组,对原数据没有影响。
array.flat(depth)
const arr1 = [1, 2, [3, 4]];
console.log(arr1.flat()); // [1, 2, 3, 4]
const arr2 = [1, 2, [3, 4, [5, 6]]];
console.log(arr2.flat(2)); // [1, 2, 3, 4, 5, 6]
Array.prototype.flatMap()
Array.prototype.flatMap()
方法首先使用映射函数映射每个元素,然后将结果压缩成一个新数组。它与 map
和 flat
的组合效果相同。
array.flatMap(callback(currentValue, index, array), thisArg)
const arr = [1, 2, 3, 4];
console.log(arr.flatMap(x => [x * 2])); // [2, 4, 6, 8]
console.log(arr.flatMap(x => [[x * 2]])); // [[2], [4], [6], [8]]
ES6 提供了许多强大的内置对象扩展方法,使得开发者能够更加高效地处理数组、字符串和对象。通过掌握这些方法,可以显著提升代码的可读性和性能。希望本文的介绍和示例能够帮助你更好地理解和使用这些方法。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。