You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dojo继承方法调用报错:严格模式下无法访问caller等属性

解决Dojo inherited调用时的严格模式错误

这个问题的核心在于严格模式对arguments对象的限制,以及Dojo inherited方法的工作机制,我来拆解一下:

为什么会报错?

  1. 严格模式的约束:在ES严格模式下,你不能访问不属于当前执行函数的arguments对象的caller、callee这类内部属性——而你把foo函数的arguments赋值给somevariable后,在异步回调里使用时,这个arguments已经脱离了foo的执行上下文,属于"外来"的arguments,访问它的内部属性就触发了报错。
  2. Dojo inherited的依赖:inherited(arguments)这种写法,本质是让Dojo通过arguments的caller属性找到当前执行的方法(也就是foo),进而定位到父类的同名方法。当arguments脱离原函数上下文后,这个机制就失效了,还触发了严格模式的限制。

那为什么直接写inherited(arguments)没问题?因为此时的arguments是回调函数自己的arguments(而不是foo的),Dojo在处理空/无关的arguments时,可能会 fallback 到其他逻辑(比如自动检测当前调用栈,或者默认调用无参的父类方法),所以没报错。但其实这可能不是你真正想要的——你大概率是想把foo的参数传递给父类方法吧?

解决方法

方法1:把参数转为数组保存,手动指定方法名调用

最稳妥的方式是提前把foo的参数转换成普通数组,然后在回调里用this.inherited的显式写法(指定父类方法名+参数数组):

foo: function(){
  // 将arguments转为普通数组,脱离原arguments对象的限制
  const fooParams = Array.from(arguments);
  when(service call here, function (response) {
    // 显式调用父类的foo方法,传递保存的参数
    this.inherited("foo", fooParams);
  }.bind(this));
}

方法2:提前获取父类方法的引用

如果你需要更灵活的调用,可以提前获取父类foo方法的引用,然后在回调里直接调用:

foo: function(){
  const fooParams = Array.from(arguments);
  // 获取父类的foo方法
  const parentFoo = this.getInherited("foo");
  when(service call here, function (response) {
    // 用apply调用父类方法,绑定当前this并传递参数
    parentFoo.apply(this, fooParams);
  }.bind(this));
}

方法3:如果不需要传递参数

如果父类foo方法不需要参数,直接调用this.inherited("foo")即可,完全不用碰arguments:

foo: function(){
  when(service call here, function (response) {
    this.inherited("foo");
  }.bind(this));
}

这样就能避开严格模式对arguments的限制,同时正确调用父类方法啦。

内容的提问来源于stack exchange,提问作者zamil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:18:14