JavaScript内置对象学习-A

Array

静态方法

  • from( arrayLike[, mapFn[, thisArg]] ) 对一个类似数组或者可迭代的对象创建一个新的浅拷贝的数组实例
const arrLike = {
    
    0: 0,1:1,2: {
    
    a:1,b:2},length: 3};
Array.from(arrLike); // [0,1,{a:1,b:2}]
  • isArray 确定传递的值是否是一个 Array ,返回布尔值
  • of(element0[, element1[, …[, elementN]]]) 创建一个具有可变参数的新数组实例
Array(2); // [ , ,] 注意单个参数当做整个数组的长度
Array.of(1, 2, 3); //[1, 2, 3]

实例属性

  • length 数组中元素的长度

实例方法

  • at([index]) 返回索引处的元素(不存在的返回 undefined );传递一个负索引时,从数组末尾开始相对索引
const arr = [0, 1, 2];
arr.at(-1); // 2
  • concat(value0, … , valueN) 合并多个数组,返回一个新的数组
const arr1 = [[0]];
const arr2 = [1, [2]];
const arr3 = arr1.concat(arr2); // [[0], 1, [2]]
  • copyWithin([target[, start[, end]]]) 浅复制数组的一部分到同一数组的另一个位置,并返回数组,不会改变数组原长度
    参数为负值时从末尾开始计算
const arr = [0, 1, 2, 3];
arr.copyWithin(3,2); // [0, 1, 2, 2] 
arr; // [0, 1, 2, 2] 
// or
const arr = [0, 1, 2, 3];
arr.copyWithin(0, 1, 3); // [1, 2, 2, 3] 
  • entries() 返回一个新的 Array Iterator 对象
const arr = ['a', 'b'];
const iterator = arr.entries();
iterator.next().value // [0, 'a']

for(let item of iterator){
    
    
	console.log(item)
}
// [0, 'a']
// [1, 'b']
  • every(callback(element[, index[, array]])[, thisArg]) 测试数组中所有元素是否通过指定函数的测试,返回布尔值
const isBelowThreshold = (currentValue) => currentValue < 40;
const arr = [10, 20];
arr.every(isBelowThreshold) // true
  • fill(value[, start[, end]]) 用一个固定值填充数组中起始索引到结束索引(不包括)
const arr = [1, 2, 3];
arr.fill(1, 1) // [1, 1, 1]
  • filter(callback(element[, index[, array]])[, thisArg]) 返回一个新数组,包含通过测试的所有元素
const arr = [1, 2, 3];
arr.filter(item => item > 2); // [3]
  • find(callback[, thisArg]) 返回数组中满足测试函数的第一个元素的值
const arr = [1, 2, 3];
arr.find(item => item > 2);  // 3
  • findIndex(callback[, thisArg]) 返回数组中满足测试函数的第一个元素的索引
const arr = [1, 2, 3];
arr.findIndex(item => item > 2);  // 2
  • flat([depth]) 按照指定深度递归遍历数组,并将所有元素和遍历到子数组中的元素合并成 一个新的数组返回
const arr = [0, 1, 2, [[[3, 4]]]];
arr.falt(2);  // [0, 1, 2, [3, 4]]
  • flatMap(callback(element[, index[, array]])[, thisArg]) 先映射函数隐射每一个元素,然后合并,返回新数组
const arr = [1, 2, 3];
arr.flatMap(item => [ item * 2 ]);  // [[2], [4], [6]]
  • forEach(callback(currentValue [, index [, array]])[, thisArg]) 对数组中每一个元素执行一次给的的函数
const arr = [1, 2, 3];
arr.forEach(item => console.log(item))
  • includes(valueToFind[, fromIndex = 0]) 判断一个数组中是否包含一个指定的值,返回布尔值
const arr = [1, 2, 3];
arr.includes(2);  // true
  • indexOf(searchElement[, fromIndex = 0 ]) 返回数组中可以找到的一个给定元素的索引,不存在返回 -1
const arr = [1, 2, 3];
arr.indexOf(2);  // 1
  • join([separator = ‘,’]) 将数组所有元素连接成字符串
const arr = [1, 2, 3];
arr.join();  // 1,2,3
  • keys() 返回一个包含数组中每个索引键的Array Iterator 对象
const arr = ['a', 'b'];
const iterator = arr.keys();

for(let item of iterator){
    
    
	console.log(item)
}
// 0
// 1
  • lastIndexOf(searchElement[, fromIndex]) 逆向查找,从数组的最后一个索引开始查找
const arr = [1, 2, 3];
arr.lastIndexOf(2);  // 1
  • map(callback(currentValue[, index[, array]]) 创建一个新数组,其结果是该数组中每一个元素调用一次提供函数后的返回值
const arr = [1, 2, 3];
arr.map(item => item * 2);  // [2, 4, 6]
  • pop() 从数组中删除最后一个元素, 并返回该元素
const arr = [1, 2, 3];
arr.pop();  // 3
  • push(element1, …, elementN) 将多个元素添加到数组末尾
const arr = [1, 2, 3];
arr.push(4, 5);  
arr // [1, 2, 3, 4, 5]
  • reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue]) 对数组中的每个元素执行回调函数,汇总结果
const arr = [1, 2, 3];
arr.reduce((accum, current) => accum + current, 5);  // 5 + 1 + 2 + 3
  • reduceRight(callback(accumulator, currentValue[, index[, array]])[, initialValue]) 从右边开始执行回调函数
const arr = [1, 2, 3];
arr.reduceRight((accum, current) => accum + current, 5);  // 5 + 3 + 2 + 1
  • reverse() 颠倒当前数组中元组的位置
const arr = [1, 2, 3];
arr.reverse();
arr // [3, 2, 1]
  • shift() 移除数组中第一个元素,并返回该元素
const arr = [1, 2, 3];
arr.shift(); // 1
  • slice([begin[, end]]) 返回一个包括begin索引,不包括end索引的新数组
const arr = [1, 2, 3];
arr.slice(-2, -1); // [2]
  • some(callback(element[, index[, array]])[, thisArg]) 测试数组中至少有一个元素满足回调函数,返回布尔值
const arr = [1, 2, 3];
arr.some(item => item > 1); // true
  • sort([compareFunction]) 排序先转字符串,然后比较
const arr = [3, 2, 1];
arr.sort((a, b) => a - b);
a // [1, 2, 3]
  • splice(start[, deleteCount[, item1[, item2[, …]]]]) 删除或替换现有的元素,或者添加新元素的形式来修改原数组
const arr = [1, 2, 3];
arr.splice(2, 0, 4);
arr // [1, 2, 4, 3]
  • toLocalString([locales[,options]]) 将数组中的元素转换成特定语言环境的字符串
const arr = [1, 2, 3, new Date()]
arr.toLocalString() // 1,2,3,2021/11/26 上午1:10:10
  • toString() 返回字符转
const arr = [1, 2, 3]
arr.toString() // 1,2,3
  • unshift(element1, …, elementN) 将一个或多个元素添加到数组头部,返回该数组的新长度
const arr = [1, 2, 3]
arr.unshift(0) // 4
  • values() 返回新的Array Iterator 对象
const arr = ['a', 'b'];
const iterator = arr.values();

for(let item of iterator){
    
    
	console.log(item)
}
// a
// b

ArrayBuffer

表示通用的固定长度的原始二进制数据缓冲区,要通过类型数组对象或者DataView对象来操作
new ArrayBuffer(length) 要创建缓存区的大小,单位字节

实例属性

  • byteLength 只读属性,只在ArrayBuffer构建完成时生成,表示byte的大小

静态方法

  • isView(arg) 参数是 ArrayBuffer 的视图实例则返回 true, 例如 类型数组对象 或 DataView 对象

实例方法

  • slice(begin[, end]) 返回一个新的 ArrayBuffer,它的内容是这个ArrayBuffer的字节副本,从begin(包括),到end(不包括)

AsyncFunction

用来创建新的异步函数的对象,JavaScript 中每个异步函数都是 AsyncFunction 的对象
AsyncFunction 不是全局对象,需要Object.getPrototypeOf(async function(){}).constructor的方法来获取

function resolveAfter2Seconds(num) {
    
    
	return new Promise(resolve => {
    
    
		setTimeout(()=> {
    
    resolve(num)}, 2000)
	})
}

const AsyncFunction = Object.getPrototypeOf(async function(){
    
    }).constructor
const async4Seconds = new AsyncFunction('a', 'b', 'return await resolveAfter2Seconds(a) + await resolveAfter2Seconds(a);')
async4Seconds(10, 20).then(result => console.log(result)) // 4 Seconds print 30

Atomics

提供一组静态方法对 SharedArrayBuffer 和 ArrayBuffer 对象进行原子操作(和 Math 对象一样)

静态方法

  • add(typedArray, index, value) 将给定的值加到数组里的某个特定位置上,并返回该位置的旧值
const buffer = new SharedArrayBuffer(16);
const uint8 = new Uint8Array(buffer);
uint8[0] = 7;
Atomics.add(uint8, 0, 2); // 7
Atomics.load(uint8, 0); // 9
  • and(typedArray, index, value) 将给定的值与数组上的值进行按位与操作,并将结果赋值给数组,然后返回数组该位置上的旧值
const buffer = new SharedArrayBuffer(16);
const uint8 = new Uint8Array(buffer);
uint8[0] = 7;
Atomics.and(uint8, 0, 2); // 7
Atomics.load(uint8, 0); // 7 (0111) AND 2 (0010) = 2 (0010)
  • compareExchange(typedArray, index, expectedValue, replacementValue) 数组的值与期望值相等的时候,将给定的替换值替换掉数组上的值,然后返回旧值
const buffer = new SharedArrayBuffer(16);
const uint8 = new Uint8Array(buffer);
uint8[0] = 7;
Atomics.and(uint8, 0, 7, 2); // 7
Atomics.load(uint8, 0); // 2
  • exchange(typedArray, index, value) 用给定的值替换掉数组上的值,然后返回数组的旧值
const buffer = new SharedArrayBuffer(16);
const uint8 = new Uint8Array(buffer);
uint8[0] = 7;
Atomics.exchange(uint8, 0, 2); // 7
Atomics.load(uint8, 0); // 2
  • load(typedArray, index) 返回一个数组当中给定位置的值
  • notify(typedArray, index, count) 提醒一些在等待队列中休眠的代理,本操作仅在共享的 Int32Array 下可用
const buffer = new SharedArrayBuffer(16);
const int32 = new Int32Array(buffer);
console.log(int32[0]); // 0;
Atomics.store(int32, 0, 123);
Atomics.notify(int32, 0, 1);
  • or(typedArray, index, value) 将给定的值与数组上的值进行按位或操作,并将结果赋值给数组,然后返回数组该位置上的旧值
  • store(typedArray, index, value) 给定的值存储在数组中的指定位置,并返回该值
  • sub(typedArray, index, value) 数组中的给定位置减去给定值,然后返回数组该位置上的旧值
  • wait(typedArray, index, value[, timeout]) 确保了一个在 Int32Array 数组中给定位置的值没有发生变化(仍然是给定的值)时进程将会睡眠,直到被唤醒或超时。该方法返回一个字符串,值为"ok", “not-equal”, 或 “timed-out” 之一
  • xor(typedArray, index, value) 数组中给定位置进行一次按位异或操作,并返回该位置的旧值

猜你喜欢

转载自blog.csdn.net/u013270347/article/details/121520331