关于caller和callee的对比

本文深入探讨了JavaScript中caller和callee属性的作用与区别。caller属性返回调用当前函数的函数环境,而callee属性则指向正在执行的函数,常用于匿名函数的递归调用。在ES5严格模式下,这两个属性已被废弃。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码和结果

		function a(){
			console.log("a.caller",a.caller);
			console.log("arguments.callee",arguments.callee);
		}
		(function b(){
			a()
		})();

对比

  • caller是通过函数来调用,返回的是该函数的调用环境,也就是调用栈的最顶层(当最顶层为全局作用域时,返回null)
  • callee是通过arguments对象来调用,返回的是执行时的函数环境,通常用于匿名函数递归调用自身

 

注意

在ES5严格模式中,该方法被废弃不再使用

为啥自定义校验// 提取出来的密码校验函数 const validatePasswordMatch = (rule: any, value: string, callback: Function) => { console.log('rule:',rule) console.log('value:',value); console.log('callback:',callback); if (!formModel.value.password) { callback(new Error('密码不能为空')) } else if (value !== formModel.value.password) { callback(new Error('两次密码输入不一致')) } else { callback() // 校验通过 } }rule: 123 forgetPsw.vue:51 value: Proxy(Object) {trigger: 'blur', validator: ƒ}[[Handler]]: MutableReactiveHandler[[Target]]: Objecttrigger: "blur"validator: (rule, value, callback) => {…}length: 3name: "validatePasswordMatch"arguments: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them at validatePasswordMatch.invokeGetter (<anonymous>:3:28)]caller: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them at validatePasswordMatch.invokeGetter (<anonymous>:3:28)][[FunctionLocation]]: forgetPsw.vue:49[[Prototype]]: ƒ ()[[Scopes]]: Scopes[3][[Prototype]]: Objectconstructor: ƒ Object()hasOwnProperty: ƒ hasOwnProperty()isPrototypeOf: ƒ isPrototypeOf()propertyIsEnumerable: ƒ propertyIsEnumerable()toLocaleString: ƒ toLocaleString()toString: ƒ toString()valueOf: ƒ valueOf()__defineGetter__: ƒ __defineGetter__()__defineSetter__: ƒ __defineSetter__()__lookupGetter__: ƒ __lookupGetter__()__lookupSetter__: ƒ __lookupSetter__()__proto__: (...)get __proto__: ƒ __proto__()set __proto__: ƒ __proto__()[[IsRevoked]]: false forgetPsw.vue:52 callback: undefined
03-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值