首页 > 编程知识 正文

call是什么函数,called和call的用法区别

时间:2023-05-05 04:20:43 阅读:263941 作者:2565

1. callee arguements.callee 是什么? 就是一个指针 可以指向函数本身

1.每个函数都存在一个arguements的对象 尔callee存在于arguements
callee是arguements arguements的属性
callee的使用需要在函数内部进行访问

function fn(){}dir(fn)arguments: nullcaller: nulllength: 0name: "fn"prototype: {constructor: ƒ}[[FunctionLocation]]: VM96:1[[Prototype]]: ƒ ()[[Scopes]]: Scopes[1] function add(a) { if (a==1) return 1; return a*arguments.callee(a-1); //此时可以访问到 //arguments.callee 就等同于自身函数add } 2.caller 是什么? 指向他的执行者 caller属于函数的一个属性 function a(){ console.log(a.caller) //此时的a.caller 指向b 因为他在b函数运行 } function b(){ a() } 3.callee用处

1.递归

function add(a) { console.log(add.caller) if (a==1) return 1; return a*add(a-1); } //如果 你把函数赋值给了 别的变量var other=add;add=nullother(2)//将会报错 因为return a*add(a-1);时候 add不再是函数 function add(a) { console.log(add.caller) if (a==1) return 1; return a*arguments.callee(a-1); } //如果 你把函数赋值给了 别的变量var other=add;add=nullother(2)//正常 因为return a*arguments.callee(a-1);//时候 arguments.callee将会指向other let a=(function as() { if (a==1) return 1; return a*as(a-1); }) //因为严格模式 不支持arguement 所以可以用以上的方式替代 完成递归

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。