当前位置:网站首页>Realize a simple version of array by yourself from
Realize a simple version of array by yourself from
2022-07-07 12:30:00 【Xiaoding Chong duck!】
purpose :
Array.from Method from an array like or iteratable object to create a new , Array instance of shallow copy . ——MDN
See Array.from()
Use the syntax :
Array.from(arrayLike[, mapFn[, thisArg]])
Code :
function myArrayFrom() {
let arrayLike = arguments[0];
// Judge whether the first parameter is empty
if (arrayLike == null) {
throw new TypeError("Array.from requires an array-like object - not null or undefined");
}
let fn = arguments.lenght > 1 ? arguments[1] : undefined;
// Judge whether the second parameter is a function
if (fn && !(typeof fn === 'function')) {
throw new TypeError('Array.from: when provided, the second argument must be a function');
}
let thisArg = arguments.lenght > 2 ? arguments[2] : undefined;
let len = arrayLike.length;
let i = 0, value;
let arr = new Array(len);
while (i < len) {
value = arrayLike[i];
if (fn) {
arr[i] = !thisArg ? fn(value, i) : fn.call(thisArg, value, i);
} else {
arr[i] = value;
}
i++;
}
return arr;
}
verification :
function mySum () {
console.log(arguments);
console.log(myArrayFrom(arguments));
}
mySum(1,2,3,4,42,4,24);
/*
[Arguments] {
'0': 1,
'1': 2,
'2': 3,
'3': 4,
'4': 42,
'5': 4,
'6': 24
}
*/
/*
[
1, 2, 3, 4,
42, 4, 24
]
*/
边栏推荐
- (待会删)yyds,付费搞来的学术资源,请低调使用!
- idea 2021中文乱码
- Several methods of checking JS to judge empty objects
- sql-lab (54-65)
- 消息队列消息丢失和消息重复发送的处理策略
- 什么是ESP/MSR 分区,如何建立ESP/MSR 分区
- 跨域问题解决方案
- Sign up now | oar hacker marathon phase III midsummer debut, waiting for you to challenge
- Static comprehensive experiment
- Inverted index of ES underlying principle
猜你喜欢
(待会删)yyds,付费搞来的学术资源,请低调使用!
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
Hi3516全系统类型烧录教程
111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
How to use PS link layer and shortcut keys, and how to do PS layer link
Sign up now | oar hacker marathon phase III midsummer debut, waiting for you to challenge
Solutions to cross domain problems
Airserver automatically receives multi screen projection or cross device projection
【统计学习方法】学习笔记——提升方法
跨域问题解决方案
随机推荐
Vxlan static centralized gateway
leetcode刷题:二叉树27(删除二叉搜索树中的节点)
111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
<No. 9> 1805. Number of different integers in the string (simple)
Epp+dis learning road (2) -- blink! twinkle!
Completion report of communication software development and Application
Sort out the garbage collection of JVM, and don't involve high-quality things such as performance tuning for the time being
[pytorch practice] image description -- let neural network read pictures and tell stories
Decrypt gd32 MCU product family, how to choose the development board?
Visual studio 2019 (localdb) \mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports version 782 and earlier
解密GD32 MCU产品家族,开发板该怎么选?
leetcode刷题:二叉树24(二叉树的最近公共祖先)
Upgrade from a tool to a solution, and the new site with praise points to new value
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
leetcode刷题:二叉树26(二叉搜索树中的插入操作)
Routing strategy of multi-point republication [Huawei]
About web content security policy directive some test cases specified through meta elements
普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
千人规模互联网公司研发效能成功之路
利用栈来实现二进制转化为十进制