当前位置:网站首页>JS learning notes -- bottom implementation of array method
JS learning notes -- bottom implementation of array method
2022-07-23 23:42:00 【( σ ゚∀゚) σ..:** Ouch, good】
push – Tail add
Returns the length value of an array
Array.prototype.myPush = function(val) {
if (arguments.length && arguments.length > 1) {
for (let i = 0; i < arguments.length; i++) {
this[this.length] = arguments[i]
}
} else {
this[this.length] = val
}
return this.length
}
pop Tail delete
Only one... Can be deleted , And return the deleted element
// Tail delete
Array.prototype.myPop = function() {
// Delete the last element And return the currently deleted element
console.log('11==',this)
const val=this[this.length-1]
this.length--
return val
}
unshift Add... To the head
Return array length
Array.prototype.myUnshift = function(val) {
const len = arguments.length
const len1 = this.length
if (len1) {
// The array length is greater than 0, All elements move back
if (len) {
for (let i = len1 - 1; i >= 0; i--) {
// const temp=this[i]
this[i + len] = this[i]
}
for (let i = 0; i < len; i++) {
this[i] = arguments[i]
}
}
} else {
// The length of the array is equal to 0
if (len) {
for (let i = 0; i < len; i++) {
this[i] = arguments[i]
}
}
}
return this.length
}
shift Head delete
No parameter , Return the currently deleted element value
Array.prototype.myShift=function(){
for (var i = 0; i < this.length-1; i++) {
this[i]=this[i+1]
}
const val=this[0]
this.length--
return val
}
splice Delete Replacement elements Insert elements
Return deleted elements The first parameter Starting element coordinates , The second element Delete the number of elements , Third to third N individual , Replaced or added elements
Ongoing update
边栏推荐
- anchor free yolov1
- Solo article body contains & lt; & gt; Labels affect page styles
- 第六章、实现一个持久性适配器
- FreeRTOS personal notes - delay function
- 【Error】TypeError: expected str, bytes or os. PathLike object, not int
- pwn1_ sctf_ two thousand and sixteen
- Is Zhongyuan securities reliable? Is it legal? Is it safe to open a stock account?
- [tensorflow] check whether tensorflow GPU is available
- [Fifth space 2019 finals]pwn5
- [three-year interview and five-year simulation] Dugu Jiujian secret script of Algorithm Engineer (first six style summary) V1 version
猜你喜欢

Chapter 5: implementation of Web adapter

What is the difference between go run, go build and go install

FreeRTOS personal notes - suspend / unhook tasks

YOLOv4: Optimal Speed and Accuracy of Object Detection

DGS初识

pwn1_ sctf_ two thousand and sixteen

BUUCTF -rip

Structured Streaming 编程模型(Input Table、Result Table、Output Mode...)

strncat() strncmp()

Mobile, telecom and Unicom: fancy solution of 5g to B
随机推荐
Chinese NFT? NFR was born
[computer three-level information security] access control model
虚拟机导入iso后 Operating System not found 解决方法
solo 文章正文含有 &lt;&gt; 标签会影响到页面样式
最长递增子序列变种[深刻理解最长递增序列]
第四章、实现用例
Stm32mp1 M4 bare metal cubeide Development Guide Chapter 6 stm32cube firmware package
中原证券靠谱吗?是否合法?开股票账户安全吗?
Kirin OS and Godson environment compilation and installation of greatsql
jarvisoj_level2
Regular expressions and bypass cases
BGP基础实验
Qt | 设置部件大小 sizeHint、minimumSizeHint、sizePolicy、stretch factor
北大青鸟昌平校区:运维就业现状怎么样?技能要求高吗?
y75.第四章 Prometheus大厂监控体系及实战 -- prometheus报警设置(六)
Code generation of DGS
[SSM]前后台协议联调②
bjdctf_ 2020_ babystack
Chapter 5: implementation of Web adapter
世界最小的物质排名,灵子,弦子,夸克