粥里有勺糖

vuePress-theme-reco 粥里有勺糖    2018 - 2023
粥里有勺糖 粥里有勺糖

Choose mode

  • dark
  • auto
  • light
关于我
备战春秋
  • 心得总结
  • 校招考点汇总
  • 面经汇总
  • 复习自查
技术笔记
  • 技术教程
  • 模板工程
  • 源码学习
  • 技术概念
  • 个人作品
  • 学习笔记
计算机基础
  • 算法与数据结构
  • 操作系统
  • 计算机网络
  • 设计模式
  • 剑指offer
大前端
  • javascript
  • vue
  • html
  • css
  • 🌏浏览器专题
  • Web性能优化
  • regexp
  • node
面试
  • 问解
  • javascript
  • css
  • 手撕代码
  • 性能优化
  • 综合问题
  • 面经汇总
  • 小程序
手撕代码
  • 数据结构与算法
  • javascript
  • css
个人站点
  • GitHub (opens new window)
  • 博客园 (opens new window)
  • 掘金 (opens new window)
线上作品
  • 轻取(文件收集) (opens new window)
  • 个人图床 (opens new window)
  • 考勤小程序 (opens new window)
  • 时光恋人 (opens new window)
  • 在线简历生成 (opens new window)
留言板
Github (opens new window)
author-avatar

粥里有勺糖

285

文章

40

标签

关于我
备战春秋
  • 心得总结
  • 校招考点汇总
  • 面经汇总
  • 复习自查
技术笔记
  • 技术教程
  • 模板工程
  • 源码学习
  • 技术概念
  • 个人作品
  • 学习笔记
计算机基础
  • 算法与数据结构
  • 操作系统
  • 计算机网络
  • 设计模式
  • 剑指offer
大前端
  • javascript
  • vue
  • html
  • css
  • 🌏浏览器专题
  • Web性能优化
  • regexp
  • node
面试
  • 问解
  • javascript
  • css
  • 手撕代码
  • 性能优化
  • 综合问题
  • 面经汇总
  • 小程序
手撕代码
  • 数据结构与算法
  • javascript
  • css
个人站点
  • GitHub (opens new window)
  • 博客园 (opens new window)
  • 掘金 (opens new window)
线上作品
  • 轻取(文件收集) (opens new window)
  • 个人图床 (opens new window)
  • 考勤小程序 (opens new window)
  • 时光恋人 (opens new window)
  • 在线简历生成 (opens new window)
留言板
Github (opens new window)
  • Javascript

    • 简介
    • 获取某年某月的天数
    • 日期格式化
    • 展开多级数组
    • 判断数据类型的方案
    • 文件上传与下载
    • 柯里化
    • 闭包
    • delete
    • 垃圾回收
    • 节流与防抖
    • apply,call,bind
    • blob与file
    • Event Loop
    • Promise
    • 定时器
    • 原型与原型链
    • 作用域
    • 执行上下文栈
    • 变量对象
    • 作用域链
    • ECMAScript规范解读this
    • 执行上下文
    • 参数按值传递
    • 类数组与arguments
    • 浮点数
    • Symbol的用法
    • 箭头函数
    • 类型转换
    • 获取dom元素的方式
    • 浅拷贝与深拷贝
    • ES6+的新语法糖和方法整理
    • 学习过程中学到的一些取巧之法

类数组与arguments

vuePress-theme-reco 粥里有勺糖    2018 - 2023

类数组与arguments

粥里有勺糖 2020-04-14 大前端javascript

# 类数组与arguments

# 类数组

拥有一个 length 属性和若干索引属性的对象

示例

let arraylike = {
    0:1,
    1:'aaa',
    2:'ddd',
    length:3
}
1
2
3
4
5
6

# 读写

arraylike[1] = 'abc'
console.log(arraylike[0]) // 1
console.log(arraylike[1]) // abc
console.log(arraylike.length) // 3
1
2
3
4

# 遍历

for(let i = 0;i<arraylike.length;i++){
    console.log(arraylike[i])
}
1
2
3

# 调用数组方法

Array.prototype.slice.call(arraylike,0) // [1,'aaa','ddd']
Array.prototype.join.call(arraylike,',') // 1,'aaa','ddd'
Array.prototype.map.call(arraylike,(a)=>{
    return typeof a
}) // ['number','string','string']
1
2
3
4
5

# 转数组

Array.prototype.slice.call(arraylike,0)
Array.prototype.splice.call(arraylike,0)
Array.from(arraylike)
Array.prototype.concat.call([],arraylike)
1
2
3
4

# arguments

函数体中的Arguments对象,包括了函数的参数和其他属性

function fn(a,b){
    console.log(arguments) // {0:a,1,b,length:2}
}
1
2
3

# length属性

表示实参的长度

function fn(a,b,c){
    console.log(arguments.length)
}
fn(1) // 1
1
2
3
4

# callee属性

通过它可以调用函数自身。

function fn(){
    console.log(arguments.callee === fn) // true
}
fn()
1
2
3
4

# arguments绑定对应的参数

function fn(a,b,c){
    // 改变形参
    console.log(arguments[0]) // 666
    a = 1
    console.log(arguments[0]) // 1

    // 改变arguments
    console.log(b) // 555
    arguments[1] = 2
    console.log(b,arguments[1]) // 2 2

    // 未传入的值
    console.log(c) // undefined
    arguments[2] = 3
    console.log(c,arguments[2]) // undefined 3
}
fn(666,555)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

总结

  • 传入的参数,实参和 arguments 的值会共享
  • 没有传入时,实参与 arguments 值不会共享

# 传递参数

function foo(){
    return bar.apply(this,arguments)
}
function bar(a,b){
    return a+b;
}
console.log(foo(1,2)) // 3
1
2
3
4
5
6
7

参考

JavaScript深入之类数组对象与arguments (opens new window)

Edit this page (opens new window)
Last Updated: 2022/5/15 12:46:34