当前位置:网站首页>JS学习笔记-- 数组方法 底层实现方式
JS学习笔记-- 数组方法 底层实现方式
2022-07-23 23:38:00 【(σ゚∀゚)σ..:**哎哟不错哦】
push – 尾部添加
返回数组的长度值
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 尾部删除
只能删除一个 ,并返回删除的元素
// 尾部删除
Array.prototype.myPop = function() {
// 删除最后一个元素 并返回当前删除的元素
console.log('11==',this)
const val=this[this.length-1]
this.length--
return val
}
unshift 头部添加
返回数组长度
Array.prototype.myUnshift = function(val) {
const len = arguments.length
const len1 = this.length
if (len1) {
// 数组长度大于0,所有元素往后移动
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 {
// 数组长度等于0
if (len) {
for (let i = 0; i < len; i++) {
this[i] = arguments[i]
}
}
}
return this.length
}
shift 头部删除
无参数,返回当前删除的元素值
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 删除 替换元素 插入元素
返回删除的元素 第一个参数 起始元素坐标,第二个元素 删除元素的个数,第三个至第N个 ,替换或新增的元素
持续更新中
边栏推荐
- Arrayslist and sequence table -- Simulation Implementation
- Qt创建背景遮罩,弹出子窗口,父窗口背景变黑变暗
- [CTF] Tiange team writeup - the first digital space security attack and defense competition (Preliminary)
- solo 文章正文含有 &lt;&gt; 标签会影响到页面样式
- PHP(2)
- 在openEuler社区开源的Embedded SIG,来聊聊它的多 OS 混合部署框架
- [SSM]前后台协议联调②
- Sql156 average completion rate of each video
- The most complete 2022 Android interview questions in history
- Chapter 6: implement a persistence adapter
猜你喜欢
随机推荐
[computer three-level information security] access control model
[tensorflow] check whether tensorflow GPU is available
Solidity-delegateCall插槽冲突分析与解决
Error handling of DGS
[web vulnerability exploration] SQL injection vulnerability
[redis] redis installation and client redis cli use (batch operation)
[Fifth space 2019 finals]pwn5
FreeRTOS personal notes - delay function
YOLOv4: Optimal Speed and Accuracy of Object Detection
BGP basic experiment
Software architecture
ArraysList 与顺序表 ——模拟实现
Strncat () strncmp ()
DDD thinking structure learning
DGS之代码生成
Analysis and resolution of slot conflict in solid delegatecall
Galaxy Securities opens an account online. Is it safe to open an account on your mobile phone
PHP(2)
Ubtun update source
Code generation of DGS




![[tensorflow] check whether tensorflow GPU is available](/img/27/e2b21b0a0cecdff27ddd2af6e34949.png)




