Is it possible to modify the parameters of a function using a different JavaScript function?

I am attempting to modify function arguments within another function and then pass them back to the calling function. My approach is as follows:

function func(a,b,c) {
    console.log(arguments, a, b, c); //[1, true, "a"] 1 true "a"
  arguments[0] = 2;
    console.log(arguments, a, b, c); //[2, true, "a"] 2 true "a"
  arguments = ArgumentsToNumber.apply(this, arguments);
  console.log(arguments, a, b, c); //[2, 1, NaN] 2 true "a"
}

function ArgumentsToNumber() {
    for (let i = 0; i < arguments.length; i++) {
    arguments[i] = Number(arguments[i]);
  }
  return arguments;
}

func(1, true, 'a');

The goal is to make changes in variables a, b, and c.

Answer №1

Make sure to provide the Arguments object itself rather than treating it like an array in apply:

function example(a, b, c) {
  console.log(a, b, c); // 1, true, "a"
  arguments[0] = 2;
  console.log(a, b, c); // 2, true, "a"
  convertArgumentsToNumber(arguments);
  console.log(a, b, c); // 2,    1, NaN
}
function convertArgumentsToNumber(args) {
  for (let i = 0; i < args.length; i++)
    args[i] = +args[i];
}
example(1, true, 'a');

Keep in mind that modifying arguments in a different function can impact performance negatively.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Seeking assistance for my dissertation assignment: electron, express, and SQLite3

I am currently dedicated to my thesis project, which involves designing an educational desktop video game for both public and private schools within my city. Being a fan of electron, I thought it would be a great idea to develop my app using this platform ...

What is the best approach for retrieving data from MongoDb with mongoose?

I am currently in the early stages of learning MongoDB and mongoose. At this point, my database structure looks like this: database -> skeletonDatabase collection -> adminLogin When I execute db.adminLogin.find() from the command line, the output ...

Using an iframe with THREE.js and WebGlRenderer can result in the domElement's getBoundingClientRect method returning 0

I encountered an issue with my code: ... renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); renderer.setSize(WIDTH, HEIGHT); ... controls = new TrackballControls(camera, renderer.domElement); Strange behavior occurs when I r ...

Utilizing vue.js 3 to implement dual @click events on a single HTML element

I need to assign 2 mouse click events to a div element. One should be triggered by holding down the shift key while clicking, and the other by a regular click, like this: <div @click.shift="aaa()" @click="bbb()">...</div> T ...

Transferring functions between files in Protractor using JavaScript

I've developed a file with several functions that I want to utilize in various E2E tests, but I'm having trouble getting them to work. Here's the situation: Within my TestingFunc.js file, I have defined the following: var Test ...

There seems to be an issue with the Y-axis not refreshing correctly in Billboard.js charts

Billboard.js allows for custom minimum and maximum values to be set for the Y-axis on a generated chart. An issue I am facing is that when these values are adjusted, the axis does not align closely with the new input values. Take this screenshot as an ex ...

Angular.js: how to filter an ng-repeat by a missing key/value pair

How can I filter the ng-repeat to only display rows where all key/value pairs are present? HTML <div ng-app="myApp" ng-controller="oneController"> <table class="table table-bordered table-hover"> <thead> <tr> <th> ...

Creating a fresh dataset using a 2D array in node-gdal-async

I'm facing a challenge in JavaScript where I need to create a new dataset from a 2D array. Despite my efforts, I can't seem to figure out the necessary steps from the documentation. It seems that in order to create this new dataset, I must utili ...

What is the best method to retrieve the current time in minutes using the Timer component?

I have integrated the react-timer-hook library into my Next.js application to display a timer. The timer is functioning correctly, but I am encountering an issue where I cannot retrieve the current elapsed time in minutes during the handle form event. My g ...

Mantine Props Extension Not Displaying in Storybook Vite

It's frustrating that I can only see my defined props in Storybook and not the Mantine props. How can I display all the Mantine props along with my own? Here are my type files: import { InputVariant, TextInputProps as MantineTextInputProps, } fro ...

How can jQuery verify if the event outcome matches a specific element?

Check out the code snippet below: $('html').on("click", function (e) { if(!$(e).is($element)) { hideResults(); } }); I attempted to use this code, but unfortunately it did not yield the desired outcome. The element in question i ...

Reanimated 2 couldn't generate a worklet - did you accidentally overlook adding Reanimated's babel plugin? The JavaScript engine in use is Hermes

I'm encountering an issue with React-native-Reanimated. I keep getting the error message "Reanimated 2 failed to create a worklet, maybe you forgot to add Reanimated's babel plugin?, js engine: hermes" However, I've already followed the st ...

The function 'ShouldWorkController' was expected but is not defined, receiving undefined instead

Whenever I attempt to connect a controller to a template using the angular-ui-router $stateProvider, I encounter this error message: 'ShouldWorkController' is not a function. Got undefined. Surprisingly, when I define the controller within the ...

"When a new field is added using select2 ajax, the previous text in the fields will vanish

Utilizing the select2 plugin on my input box to execute AJAX searches for items in the database based on user input was functioning well. I could search for items and select them when available. However, a problem arose whenever new rows were added to my t ...

If the input field is empty, there is no action required. However, if the input field contains a value, it must be checked to ensure it is a numeric value. If

I'm attempting to create a form that automatically adds up input fields when they lose focus, displaying the sum in an inactive "Total:" field. I want to avoid running any calculations if a user clicks on an input field and then moves away without ent ...

Dealing with ReactJs Unhandled Promise Rejection: SyntaxError - Here's the Solution

Struggling to use the Fetch API in ReactJS to retrieve a list of movies. Encountering an issue, can anyone offer assistance? fetch("https://reactnative.dev/movies.json", { mode: "no-cors", // 'cors' by default }) ...

The GET request is returning a null array instead of the expected array contents

I am new to this, but I am trying to understand why my GET request is returning an empty array even though I am sure that the Mongo database collection is not empty. Each word form in the WordForm collection has a "lexicalform" key that refers to a Lexicon ...

Endless cycle of creating new windows upon clicking

On my overview page, I display data in a table format. When a user clicks on a row, a pop-up window opens. However, the issue arises when the pop-up window keeps reloading endlessly until it freezes. This is the code for the overview: <tbody> & ...

Exploring jQuery's parent selector for traversing the DOM

I am currently working with an array that inserts articles into my website. I have a specific requirement where, upon clicking the title (h3), I need to display more information based on the article's index. To achieve this, I believe I should travers ...

What is the best way to eliminate leading and trailing spaces from a text input?

I'm currently working on troubleshooting a bug in an AngularJS application that involves multiple forms for submitting data. One of the issues I encountered is that every text box in the forms is allowing and saving leading and trailing white spaces ...