当前位置:网站首页>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
边栏推荐
- Sql156 average completion rate of each video
- 虚拟机导入iso后 Operating System not found 解决方法
- Esp8266 nodemcu - get real-time weather from Suning API
- 中原证券靠谱吗?是否合法?开股票账户安全吗?
- Application of merging and sorting thought
- 第四章、实现用例
- Galaxy Securities opens an account online. Is it safe to open an account on your mobile phone
- pwn1_ sctf_ two thousand and sixteen
- BGP选路,MPLS
- DGS first acquaintance
猜你喜欢
随机推荐
BGP基础实验
BGP选路,MPLS
ciscn_2019_n_1
在openEuler社区开源的Embedded SIG,来聊聊它的多 OS 混合部署框架
DGS的错误处理
48: Chapter 5: develop admin management service: 1: create sub project [imooc news dev Service Admin], management service module;
Tensorflow one layer neural network training handwritten digit recognition
网络安全课堂作业
Kirin OS and Godson environment compilation and installation of greatsql
[redis] redis installation and client redis cli use (batch operation)
第四章、实现用例
DGS初识
Stm32mp1 M4 bare metal cubeide Development Guide Chapter 6 stm32cube firmware package
Chapter 5: implementation of Web adapter
ubtun 更新源
Solidity-delegateCall插槽冲突分析与解决
TOPSIS method (matlab)
C # introductory series (XXVIII) -- query syntax of LINQ
【攻防世界WEB】难度五星15分进阶题:bug
BUUCTF -rip





![[第五空间2019 决赛]PWN5](/img/51/03e6078961a8eab991fa08bb178a7b.png)
![[three-year interview and five-year simulation] Dugu Jiujian secret script of Algorithm Engineer (first six style summary) V1 version](/img/c1/d8b9a4e72eb4e084a77fc676f52f8f.png)

