×
  • Web前端首页
  • Js&JQuery
  • 分析脱坑之处:了解“ReferenceError: deprecated caller or arguments usage”的坑源

分析脱坑之处:了解“ReferenceError: deprecated caller or arguments usage”的坑源

作者:Terry2023.08.17来源:Web前端之家浏览:2677评论:0
关键词:js

分析脱坑之处:了解“ReferenceError: deprecated caller or arguments usage”的坑源

今天不小心又掉进坑了,来来吧,看看是什么坑吧。

错误提示:

Warning: ReferenceError: deprecated caller usage (Firefox)
Warning: ReferenceError: deprecated arguments usage (Firefox)
TypeError: 'callee' and 'caller' cannot be accessed in strict mode. (Safari)

错误类型

仅在严格模式下出现的 ReferenceError 警告。JavaScript 的执行将不会停止。

发生了什么?

在 strict mode 中,Function.caller 和 Function.arguments 属性是不该使用的。它们都是已经被废弃的了,因为这两者泄露了函数的调用者,是不标准的,难于优化和有这潜在的性能问题。

实例

废弃的 function.caller or arguments.callee.caller

Function.caller 和 arguments.callee.caller 都是已废弃的。

"use strict";

function myFunc() {
  if (myFunc.caller == null) {
    return "The function was called from the top!";
  } else {
    return "This function's caller was " + myFunc.caller;
  }
}

myFunc();
// Warning: ReferenceError: deprecated caller usage
// "The function was called from the top!"

Function.arguments

Function.arguments 已被废弃。

"use strict";

function f(n) {
  g(n - 1);
}

function g(n) {
  console.log("before: " + g.arguments[0]);
  if (n > 0) {
    f(n);
  }
  console.log("after: " + g.arguments[0]);
}

f(2);

console.log("returned: " + g.arguments);
// Warning: ReferenceError: deprecated arguments usage

了解完,是不是懵逼了,其实是自己技术没更新迭代啊。

您的支持是我们创作的动力!
温馨提示:本文作者系Terry ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://jiangweishan.com/article/jskeng20230817.html

网友评论文明上网理性发言 已有0人参与

发表评论: