ES6的内置对象扩展方法怎么用

发布时间:2022-07-04 13:49:19 作者:iii
来源:亿速云 阅读:106

ES6的内置对象扩展方法怎么用

ES6(ECMAScript 2015)引入了许多新的内置对象扩展方法,这些方法极大地增强了JavaScript的功能性和易用性。本文将介绍一些常用的ES6内置对象扩展方法,并通过示例展示如何使用它们。

1. 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 }

2. Array.prototype.includes()

Array.prototype.includes() 方法用于判断数组是否包含某个特定的元素,返回 truefalse

语法

array.includes(searchElement, fromIndex)

示例

const array = [1, 2, 3];

console.log(array.includes(2)); // true
console.log(array.includes(4)); // false

3. String.prototype.startsWith()

String.prototype.startsWith() 方法用于判断字符串是否以指定的子字符串开头,返回 truefalse

语法

str.startsWith(searchString, position)

示例

const str = 'Hello, world!';

console.log(str.startsWith('Hello')); // true
console.log(str.startsWith('world', 7)); // true

4. String.prototype.endsWith()

String.prototype.endsWith() 方法用于判断字符串是否以指定的子字符串结尾,返回 truefalse

语法

str.endsWith(searchString, length)

示例

const str = 'Hello, world!';

console.log(str.endsWith('world!')); // true
console.log(str.endsWith('Hello', 5)); // true

5. String.prototype.includes()

String.prototype.includes() 方法用于判断字符串是否包含指定的子字符串,返回 truefalse

语法

str.includes(searchString, position)

示例

const str = 'Hello, world!';

console.log(str.includes('world')); // true
console.log(str.includes('goodbye')); // false

6. 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

7. 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

8. 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]

9. 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]

10. Array.prototype.flatMap()

Array.prototype.flatMap() 方法首先使用映射函数映射每个元素,然后将结果压缩成一个新数组。它与 mapflat 的组合效果相同。

语法

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 提供了许多强大的内置对象扩展方法,使得开发者能够更加高效地处理数组、字符串和对象。通过掌握这些方法,可以显著提升代码的可读性和性能。希望本文的介绍和示例能够帮助你更好地理解和使用这些方法。

推荐阅读:
  1. C#的扩展方法
  2. es6数值的扩展方法有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

es6

上一篇:Vue 2.7的功能有哪些

下一篇:C#怎么生成putty格式的ppk文件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》