当前位置:网站首页>JS學習筆記-- 數組方法 底層實現方式
JS學習筆記-- 數組方法 底層實現方式
2022-07-23 23:41: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個 ,替換或新增的元素
持續更新中
边栏推荐
- 关于使用 Jackson 解析 JSON 你需要知道的一切
- Operating system not found solution after importing ISO into virtual machine
- 复制客服微信号,前往微信添加,拨打电话
- y75.第四章 Prometheus大厂监控体系及实战 -- prometheus报警设置(六)
- 第五章、实现Web适配器
- DGS之Mutations
- DDD thinking structure learning
- Analytic hierarchy process (matlab)
- 为什么大多数人都认为编程很难?
- Chapter 6: implement a persistence adapter
猜你喜欢
随机推荐
虚拟机导入iso后 Operating System not found 解决方法
Chapter 4: implementation use cases
Matlab Foundation
How are you preparing for the Android golden nine silver ten interview? The latest Android Interview Questions Summary helps you prepare for the war
The most complete 2022 Android interview questions in history
Software architecture
2022-7-22 face review + simple topic sorting
Kirin OS and Godson environment compilation and installation of greatsql
归并排序思想应用
BGP basic experiment
How to migrate databases in the flask framework
Practical learning of SQL statements
Chapter 6: implement a persistence adapter
【三年面试五年模拟】算法工程师的独孤九剑秘籍(前六式汇总篇)V1版
第七章、测试架构元素
Detailed explanation of pseudo instructions in assembly language (with examples)
strncat() strncmp()
史上最全的2022年版Android面试题
【Error】TypeError: expected str, bytes or os. PathLike object, not int
FreeRTOS personal notes - delay function





![[redis] redis installation and client redis cli use (batch operation)](/img/d7/7500e99bc3cf172f895a47aec0b44c.png)

![[SSM]前后台协议联调①](/img/47/c1016c5c5e4ffc0d6cc93cf50d52df.png)

