`arguments.callee` 如何引用匿名函数? [英] How does `arguments.callee` refer to anonymous functions?

查看:22
本文介绍了`arguments.callee` 如何引用匿名函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一个脚本来快速告诉我页面上有多少 html 评论以及它们的内容是什么.使用匿名函数进行递归 DOM 遍历似乎很合适:

A script was needed to quickly tell me how many html comments there are on the page and what their contents are. Using an anonymous function for recursive DOM traversal seemed appropriate:

var comments = []; //set up an array where comment contents will be copied to

(function(D) {
  if (8===D.nodeType) comments.push(D.nodeValue); //check if node is a comment
  D=D.firstChild;
  while (D) {
    arguments.callee(D); //recursively look for comments...
    D=D.nextSibling; //...and remember to iterate over all children of any node
  }
})(document);

console.log(comments.join("\r\n")); //list all comments

Fiddle 按预期工作,但我很好奇它是否真的相同 函数被反复调用,或者是否有多个对原始函数的引用,或者是否有多个相同的函数被调用......毕竟没有命名引用,那么遍历过程中它会如何工作更深?我想我可以通过将 以下代码添加到 while (D) {...}

Fiddle works as expected, but I was curious if it was really the same function called over and over, or were there multiple references to the original function called, or were there multiple identical functions called... After all, there has been no named reference made, so how would it work as the traversal goes deeper? I thought I may be able to check that by adding the following code into while (D) {...}

//tmpCallee has been declared
if (tmpCallee) {
  console.warn(arguments.callee === tmpCallee);//true
  /*great, means these should be both pointing to the same function*/
  console.log(arguments.callee === arguments.caller);//false
  /*wait, what? didn't we just establish above that 
    all of our functions called recursively would be the same?*/
  console.log(arguments.caller);//undefined... but it was called recursively!
  console.log(arguments.callee);//prints our function code verbatim as it should
}
tmpCallee = arguments.callee;

我很困惑.1) 我是否真的一遍又一遍地调用同一个函数,或者是否调用了多个相同的函数,或者还有其他什么在起作用?2) 为什么 arguments.caller not 指向我们的函数?它显然是由它调用的 - 这就是递归的工作原理,不是吗?

I'm confused. 1) am I really calling the same function over and over or are there multiple identical functions called or is something else at play? 2) why does arguments.caller not point at our function? it was clearly invoked by it - that's how recursion works, no?

推荐答案

我真的一遍又一遍地调用同一个函数,还是调用了多个相同的函数,或者有其他东西在起作用?

am I really calling the same function over and over or are there multiple identical functions called or is something else at play?

是的,你只有一个函数实例,你一直在引用它.但是,您正在设置一个调用堆栈,其中将为每次调用保存局部变量(在您的情况下为参数 D).

Yes, you have only one function instance, which you refer to all the time. However, you are setting up a call stack in which local variables (in your case the argument D) would be saved for every invocation.

为什么arguments.caller 不指向我们的函数?

why does arguments.caller not point at our function?

arguments 对象上没有 caller 属性,它是 已删除.您可能指的是 caller 属性 的函数对象,这是非标准的但仍然可用(尽管在严格模式以及 argments.callee).

There is no caller property on the arguments object, it was removed. You probably meant the caller property of the function object, which is non-standard but still usable (although forbidden in strict mode as well as argments.callee).

这篇关于`arguments.callee` 如何引用匿名函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆