Categories
JavaScript Answers

How to Fix the ‘ReferenceError: deprecated caller or arguments usage’ Error in Our JavaScript App?

Spread the love

Sometimes, we may run into the ‘ReferenceError: deprecated caller or arguments usage’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘ReferenceError: deprecated caller or arguments usage’ when we’re developing JavaScript apps.

Fix the ‘ReferenceError: deprecated caller or arguments usage’ When Developing JavaScript Apps

To fix the ‘ReferenceError: deprecated caller or arguments usage’ when we’re developing JavaScript apps, we should make sure we aren’t trying to access the Function.caller or arguments.callee.callers properties in our JavaScript code.

They’re both deprecated properties that will be removed in the future.

For instance, we should write code like:

'use strict';

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

myFunc();

Also, we shouldn’t access the Function.arguments property in our code:

"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);

We should get function arguments from the signature of the function directly or with the rest operator:

"use strict";

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

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

f(2);

args is an array with all the arguments g is called with.

Conclusion

To fix the ‘ReferenceError: deprecated caller or arguments usage’ when we’re developing JavaScript apps, we should make sure we aren’t trying to access the Function.caller or arguments.callee.callers properties in our JavaScript code.

They’re both deprecated properties that will be removed in the future.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *