0

I have an object that looks like below. Basically it contains properties that call a method. What I would like to be able to do is, instead of having DEVELOPER twice (Once for the property name and once for the parameter value), I'd like to get the current property of what was called in order to get this.

Also I don't want to pass in 'DEVELOPER' as a parameter in the initial call because I want intellisense to pick it up.

return {
   DEVELOPER: function ()
            {
                return getEmailRecipients("DEVELOPER")
            }
}
 //it get's called like this.
 emailGroups.DEVELOPER();

Essentially I'd like to do something like

return {
   DEVELOPER: function ()
            {
                return getEmailRecipients({this.currentPropName}) //Which would equal DEVELOPER.
            }
}

If there is a better way to do this, I am all ears.

Thanks in advance!

Godrules500
  • 467
  • 6
  • 25
  • This would make more sense to me: `return { getMail: function (who) { return getEmailRecipients(who) } }` and call it like `emailGroups.getMail("DEVELOPER");` - what does intellisense do that makes it clever? – mplungjan Jun 21 '18 at 12:46
  • I only want whomever develops this, including myself, to only have a select preset of email groups to choose from. If it is parameter/text based, user errors are going to happen. If it is a list that will only allow them select predefined groups, then it'll prevent the human error. At least from being over looked and deployed without realizing it. – Godrules500 Jun 21 '18 at 13:58

1 Answers1

0

As far as I know, this is not possible, or at least it's not built-in.

You can use the function name as defined in arguments.callee like this...

arguments.callee.name

But the name property of functions is not well supported.

You might try something like this...

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

But that only works for one item in an object, and only the first item.

You might also try defining the object you return as a variable, and loop over that object giving every item a new property that contains the item name.

Like so...

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

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

// Return data variable as emailGroups
return data;
Coffee'd Up Hacker
  • 1,356
  • 11
  • 23
  • So the arguments.callee.name works! My hesitance with using it is you said name is not very well supported. Do you know what doesn't support the name property? – Godrules500 Jun 21 '18 at 14:03
  • Man! It doesn't work in i.e. Worked in chrome, but ie is sadly a no go. – Godrules500 Jun 21 '18 at 14:12
  • @Godrules500 Yeah, IE doesn't support the name property, and if you were to use a Chrome or Firefox from 5-10 years ago, they likely wouldn't support it either. So your next best option is the third solution I mention, giving the function a name property manually. – Coffee'd Up Hacker Jun 22 '18 at 08:52