惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
量子位
T
Tailwind CSS Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
云风的 BLOG
云风的 BLOG
K
Kaspersky official blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LangChain Blog
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
S
Secure Thoughts
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
T
Troy Hunt's Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
T
Tor Project blog
T
The Blog of Author Tim Ferriss
I
Intezer
P
Privacy & Cybersecurity Law Blog
美团技术团队
N
Netflix TechBlog - Medium
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Attack and Defense Labs
Attack and Defense Labs
T
Tenable Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI

小陈同学 - 前端

自制可拖拽元素的React Hooks 面试经验分享 工具包分享-通用素材类 工具包分享-基础验证类 《Vue学习》学习源码手撸简易Vue 《Vue学习》设计模式探索 《Vue学习》数据响应式原理 Doc纯文本迁移到Doc表格中 iView-Admin基础版后台管理框架改写
JS中Arguments对象
Caleb · 2020-07-14 · via 小陈同学 - 前端

arguments 是函数内部可索引的类数组对象,用于访问所有实参,可转数组并支持 callee 与 length 属性。

描述

arguments 是一个对应于传递给函数的参数的类数组对象。

arguments对象是所有(非箭头)函数中都可用的局部变量。你可以使用arguments对象在函数中引用函数的参数。此对象包含传递给函数的每个参数,第一个参数在索引 0 处。例如,如果一个函数传递了三个参数,你可以以如下方式引用他们:

arguments[0];
arguments[1];
arguments[2];

当然参数也可以被设置:

arguments[1] = "new value";

arguments对象不是一个 Array。它类似于 Array,但除了 length 属性和索引元素之外没有任何Array属性。例如,它没有 pop 方法。但是它可以被转换为一个真正的Array

var args = Array.prototype.slice.call(arguments);
var args = [].slice.call(arguments);

// ES2015
const args = Array.from(arguments);
const args = [...arguments];

属性

arguments.callee;

指向参数所属的当前执行的函数。
指向调用当前函数的函数。(Function 对象的正文)

arguments.length;

传递给函数的参数数量。

arguments[@@iterator]

返回一个新的Array 迭代器 对象,该对象包含参数中每个索引的值。

特点

1.arguments 对象和 Function 是分不开的。

2.因为 arguments 这个对象不能显式创建。

3.arguments 对象只有函数开始时才可用。

拓展

length 属性

遍历参数来求和。

function add() {
  var sum = 0,
    len = arguments.length;
  for (var i = 0; i < len; i++) {
    sum += arguments[i];
  }
  return sum;
}

add(); // 0
add(1); // 1
add(1, 2, 3, 4); // 10

callee 属性

实现递归函数。

function sum(n) {
  if (1 == n) {
    return 1;
  } else {
    return n + arguments.callee(n - 1); //6 5 4 3 2 1
  }
}

sum(6); // 21

当然直接使用匿名函数也是可以的:

var sum = (function (n) {
  if (1 == n) {
    return 1;
  } else {
    return n + arguments.callee(n - 1); //6 5 4 3 2 1
  }
})(6);

console.log(sum); // 21

小知识

当使用 arguments 进行函数传递时,会有一些注意的地方,下面有一道面试题的例子:

var length = 10;
function fn() {
  console.log(this.length);
}

var obj = {
  method: function (fn) {
    fn();
    arguments[0]();
  },
};

obj.method(fn, 1);

那么应该输出什么呢? 答案是:

输出结果如下:
10
2

解析:

1.第一次输出的是 10 ,是因为执行了 method 中的第一个 fn() 函数,这时打印出来的 length 指的是 window 中定义的 length 。故而打印出 10 。
2.第二次输出了 2 ,这时候执行了 method 中的第二句。arguments[0]() ( arguments[0] ==> fn() ),此时的 this 指向到了arguments对象上。所以输出值为arguments的长度。

作者: 文章链接: https://reinness.com/posts/70 版权声明:  本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自小陈同学