Is there a way to retrieve the parameters of a function that I am passing as an argument to another function?

As I strive to retrieve the arguments from the function passed as a parameter in order to ensure they are properly forwarded, an error has emerged:

TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them

How can I access the arguments for the func so that when calling it, I can successfully pass the message through?

UPDATE: Could it be that my struggle lies in why I am unable to log func.arguments?

function a(func){
  console.log(func.arguments)
  return function(){
    func.apply(null, func.arguments);
  }
}

function log(message){
  console.log(message)
}

a(log.bind('hello'))()

Answer №1

To achieve this, you can return an object with the desired parameters from the function that is being passed.

function customFunction(func) {
    console.log(func.arguments);

    // The following block of code is not necessary since you have already executed the function before passing it.
    //return function() {
    //   func.apply(null, func.arguments);
    //}
}

function log(message) {
    console.log(message);
    return {
        arguments: arguments // Store the arguments and return them
    };
}

// You are not directly passing the function here as written, but calling it and returning its value instead.
// We return { arguments: arguments } so the "customFunction" can access its properties.
customFunction(log("hello"));

Answer №2

It appears that you are aiming to intercept a function by using a "logging" function in this scenario.

If this is the case, there is an alternative method that involves utilizing ES6 rest parameters --

const logFunction = mainFunc => (...args) => { 
    console.log(...args); 
    return mainFunc(...args);
};

const addition = (a, b) => a + b;

logFunction(addition)(2, 3); // This will display '2 3' and sum up to 5

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

Update the pageExtensions setting in Next.js to exclude building pages with the file extension *.dev.*

I am currently working on a project using Next.js version v12.3, and I have encountered an issue related to excluding page files with a *.dev.* extension from the build process. In my configuration file next.config.js, I have configured the pageExtensions ...

Tips for including type definitions when adding elements to an array in TypeScript

Having trouble avoiding the use of 'any' as the type definition when pushing elements into an array? I attempted to specify the type but encountered an error. Here is a snippet of the code: interface answerProps { state: string; answer: s ...

Having two identical select2 forms on the same page

Integrating two select2 multi-value select boxes into my Rails application is proving to be a challenge. While the top form functions correctly, the bottom one refuses to work as expected. Despite my attempts at changing IDs and adding new JavaScript cod ...

Exploring jQuery.each: A guide to navigating JSON objects

As a beginner in working with JSON, I am struggling to iterate over a JSON response received via AJAX. My objective is to extract and loop through checkbox values retrieved from a database table such as 2,3,7,9,3. However, I am currently facing difficultie ...

Unraveling a date field from JSON with AngularJS filtering

Extracting data from JSON file shows /Date(1435837792000+0000)/ How can I format the date to appear as Oct 29, 2010 9:10:23 AM? ...

What are the steps to customize the appearance of a folder in the web browser?

Is it possible to customize the presentation of folder contents dragged into Chrome using CSS, HTML, JavaScript, or other methods? I've heard about the HTML5 file API but not sure if that's applicable in this scenario. I think it would be intere ...

Tips for resolving the error message "An issue occurred with the skill's response" within the Alexa Developer Console

Looking to develop a basic Alexa app where users can interact with voice commands, but encountering an issue with the response in the Test tab of Alexa Developer Console. The error message states "There was a problem with the requested skill's respons ...

Error: The src for the image of the double cheeseburger could not be properly parsed by the `next/image` component

Could you kindly take a moment to review this? import Image from "next/image"; import { useState, useEffect } from "react"; import "@/data/data.json"; interface propsData { dish: { id: numbe ...

What could be causing the JavaScript/jquery code in my Functions.php file to only function properly on the initial post that loads on my WordPress website?

Currently, I am in the process of developing a WordPress website where the content is refreshed weekly. This means that all posts, media, and files are removed from the WP environment every week and replaced with new content. One of the key components of ...

Implement a new method called "defer" to an array that will be resolved at a later time using Promise.all()

I need to manage a queue of DB calls that will be executed only once the connection is established. The DB object is created and stored as a member of the module upon connection. DB Module: var db = { localDb: null, connectLocal: (dbName) => { ...

Changing the color of a Navlink when focused

Can anyone help me modify the background color of a dropdown nav-link in Bootstrap? I am currently using the latest version and want to change it from blue to red when focused or clicked. I have included my navbar code below along with additional CSS, but ...

Expanding the capabilities of jQuery UI event handling

I am looking for a way to enhance dialog functionality by automatically destroying it when closed, without the need to add additional code to each dialog call in my current project. My idea is to override the default dialog close event. After researching ...

Implementing functionality to switch classes when clicking outside an element using Jquery

At first glance, the code snippet reveals a method to toggle the .main-navigation and apply a shadow overlay on the page simultaneously. However, my objective is to trigger the same action (closing the menu and removing the overlay) by clicking outside the ...

Getting information from Button-Group using JavaScript

I have a similar question that was previously asked here: Get Data-Values from Selected Bootstrap Button Group (Checkboxes) and Assign to Input However, my structure is different because I need a group of buttons with the option to select more than one. ...

How to use jQuery to select an element by using 'this' on a button

I have a total of 5 divs, each containing 5 different texts. <div class="text"> <%= theComment.text %> </div> I am working with node.js, MongoDB, and Mongoose. In addition to the divs, I also have a button labeled EDIT with a cl ...

Issues arise when building with Angular using `ng build`, but everything runs smoothly when using `

My Angular 5 application uses angular-cli 1.6.8 { "name": "test", "version": "0.0.0", "license": "MIT", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "karma": "ng test", "test": "jest", "test:watch": "j ...

Using jQuery to combine a conditional statement with a click event and disabling a button using the `.prop('disabled', true)` method

Looking for some assistance with this issue I've encountered. After researching on StackOverflow, I managed to find a solution to disable button clicking if the form is left empty using the $.trim(.....) code below! Success! However, adding that part ...

What is the best way to create all possible combinations of key-value pairs from a JSON array?

In my scenario, there is a JSON array with key-value pairs where the values are arrays. Here's an example: json = {foo:[1,2],bar:[3,4],pi:[5]} I am looking for a way to generate all possible combinations of these parameters for any number of keys. Th ...

Issue with redirect after submitting CakePHP form not functioning as expected

I am in the process of developing a button that will submit a form and then redirect to another page. $('#saveApp').click(function() { event.preventDefault(); $("form[id='CustomerSaveForm']").submit(); // using the nati ...

Rendering template and data from a promise in AngularJS: A comprehensive guide

Hey there, I'm diving into the world of Angular and I've been struggling for the past couple of days trying to figure out a solution for this issue. Not entirely sure if my approach is right either. My goal is to create a simple page with a dyna ...