Getting the name of the current property function in JavaScript

I have an item that is structured like this. It contains attributes that reference a function. What I am trying to achieve is to eliminate the repetition of 'DEVELOPER' (once for the attribute name and once for the parameter value) by dynamically fetching the current attribute being called.

Moreover, I prefer not to include 'DEVELOPER' as an argument in the initial invocation so that intellisense can detect it automatically.

return {
    DEVELOPER: function ()
                {
                    return getEmailRecipients("DEVELOPER")
                }
}
 //the invocation looks like this.
 emailGroups.DEVELOPER();

Essentially, I aim to accomplish something similar to the following:

return {
    DEVELOPER: function ()
                {
                    return getEmailRecipients({this.currentPropName}) //This should result in the value of DEVELOPER.
                }
}

If there is a more effective approach for achieving this task, I am open to suggestions.

Thank you in advance!

Answer №1

It seems that achieving this is not straightforward or natively supported.

One approach could be utilizing the name function from arguments.callee like so...

arguments.callee.name

However, the support for the name property of functions may be limited.

Alternatively, you might consider a workaround such as...

this.toSource().match(/\{(.*?):/)[1].trim ()

Please note that this method only works for retrieving the first item in an object.

Another idea is to define the object to be returned as a variable and iterate over it to assign each item a new property storing its name.

Here's an example:

// Object to return to emailGroups
var data = {
    DEVELOPER: function ()
    {
        return getEmailRecipients (arguments.callee.givenName)
    }
}

// Assign a property with the item name to each object item
for (var name in data) {
    if (data.hasOwnProperty (name) === true) {
        data[name].givenName = name;
    }
}

// Return the data variable as emailGroups
return data;

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

Creating a central navigation menu for a website

Currently, I am working on incorporating a menu in the center of a webpage (please note that this is not a top navigation menu). Here is the initial setup: https://i.sstatic.net/3arql.png Users are able to click on various menu items to access different ...

Encountering an unexpected token error - Karma and Webpack testing having trouble with the forward slash symbol

I have been facing some issues while setting up my angular tests. The error message I keep getting is: PhantomJS 2.1.1 (Windows 8 0.0.0) ERROR SyntaxError: Unexpected token '/' at app/components/dropdown/dropdown.spec.ts:1 PhantomJS 2.1.1 ( ...

Determining Velocity of an Object in Three.js

Can anyone help me figure out how to calculate an object's velocity in three.js? I've checked the Object3D documentation but can't seem to find anything related to velocity. Appreciate any assistance, ...

Ensure the video plays automatically without being muted

<div class="tab-img"> <video id="autplay-video" width="320" height="240" controls autoplay poster="https://solax-prod.s3.amazonaws.com/demo/Thumbnail+English-SOLAx+Video.jpg& ...

Issue TS1259: The module "".../node_modules/@types/bn.js/index"" can only be imported as the default using the 'esModuleInterop' flag

Currently, I am utilizing Hiro Stack.js which I obtained from the following link: https://github.com/hirosystems/stacks.js/tree/master/packages/transaction. For additional information, please refer to . Even when attempting to compile a fully commented out ...

Is there a way to eliminate the blue border from a Material React Modal?

I am currently using the React Material Modal and noticed that in the demo examples, there is a blue border when the modal is opened. Is there a way to remove this blue border? I have tried setting the disableAutoFocus property to "true" in the Modal Api ...

What could be causing the console to display undefined?

Can anyone help me with an issue I'm having while making an AJAX call using fetch and promises? I have successfully displayed the temperatures, but for some reason, the location is showing up as undefined. Here is the code snippet: function getWeat ...

Personalized animated Reactflow Connection Lines

My goal is to develop a personalized animated connection lines in reactflow, rather than using the default dashed line that appears when the animated: true prop is applied. I am aware that we can customize the styling by using the following code snippet: ...

Is there a way to create a stacked bar chart through iterative steps?

I am currently working with time series data stored in an array structured like this: var data = [ [{date: 2016-01-01 00:00:00, value: 52}, {date: 2016-01-02 00:00:00, value: 23}, {date: 2016-01-03 00:00:00, value: 42}, {date: 2016-01-04 00:00:00, value ...

The behavior of Quasar's q-drawer is quite unpredictable

Having made the transition from Vue.js 2 with Vuetify to Vue.js 3 with Quasar due to Vuetify not officially supporting Vue.js 3 yet, I am utilizing q-drawer and its mini property. This allows me to toggle between the mini state and the normal expanded stat ...

Automate CSS slideshow playback using JavaScript

Using only CSS, I created a basic slideshow where the margin of the element changes upon radio button click to display the desired slide. You can view the functionality in the code snippet below. Additionally, I implemented auto play for this slideshow usi ...

The mapDispatchToProps function is failing to work, throwing an error: Uncaught TypeError: _this.props.addCountdown is not a function

Currently, I am facing an issue while working on my first app. The problem arises with a form component that should send an object via an onSubmit handler. onSubmit = (e) => { e.preventDefault(); this.props.onSubmit({ title: ...

What is the best way to query for a mongoose ObjectId within feathers?

With two feathers services at hand, one for handling profiles and another for labels, the possibilities are endless. In a profile, you can have an array of ObjectId labels from other collections. Imagine the depth of connections! Now, picture this scenar ...

What is the process for transitioning from one HTML file to another in order to develop an iOS hybrid application?

I am looking to transition a webView page into a Spring-MVC structure with a hybrid format. My goal is to replace the current JSP file with an HTML file and place it within the ios project folder. The process of inserting the HTML test file was successful ...

Utilizing a Custom Hook for Updating Components in useEffect

I am facing an issue with the following code snippet: function checklogin(callback) { if (!state.user.authenticated) pushhistory("/accounts/login", function(){teamhome2_message();}); else callback(); } // TRYING TO CONVERT IT INTO ...

Utilizing Props in React to Slice and Dice Data Within a Separate Component

Currently, I am in the process of creating an about text for a profile that will include an option to expand or collapse based on its length. To achieve this, I am utilizing a function from the main home component: <AboutText text={aboutData}/> Abo ...

Sails.js seems to be malfunctioning, as it does not seem to be recognizing the term 'sails'

It seems like I'm encountering an issue with the 'sails' command not being recognized on my Windows 10 system. Despite following all the installation steps, including globally installing Sails.js through npm and ensuring Node is installed, I ...

JavaScript objects compared to arrays and JSON format

After countless hours of searching and frustration, I am still struggling to clearly define the distinctions between json, objects, and arrays (in javascript). Take a look at how I've been working with 2-dimensional data containers (trying to avoid us ...

What is the proper method for utilizing colspan within the footerData of a jqGrid?

Are you looking to customize the footer of your jqgrid as shown in the example below? I am trying to set up a custom footer for my jqgrid similar to the one displayed above. I have already enabled the footerrow:true option and used $self.jqGrid("footerDat ...

Create a random word from a single string within the data in Nuxt.js

I am in need of assistance. In my Vue Nuxtjs project, I am fetching random words generated from my backend Laravel application through an API response. I need to generate multiple random words from a single string value in the data obtained from my Axios r ...