-2

If I have the following object:

var helloWorldFunctions = {
                           'hello.world': function () { return 'hello world'},
                           'helloWorld' : function () { return 'hello world'}
                          }

I can call the second 'helloWorld' function in the object by doing:

helloWorldFunctions.helloWorld()

How can I call the first 'hello.world' function? Naturally, doing the following gives me a type error:

helloWorldFunctions.hello.world()
Kevin Lee
  • 1,370
  • 14
  • 22
  • The function has no "name". Rather the expression `hellowWorldFunctions["hello.world"]` (note it's just a normal key lookup) will return a function-object that can be called. This access (with the brackets, supplying a string as a key) is no different than accessing any other value in an object. – user2864740 Sep 18 '16 at 05:54
  • 2
    `helloWorldFunctions['hello.world']()` You can use bracket notation. – Rajaprabhu Aravindasamy Sep 18 '16 at 05:54
  • you can do `helloWorldFunctions['hello.world']()` but why would you even have a period in the name? – Amin Jafari Sep 18 '16 at 05:55
  • 1
    @user2864740: It does as of ES2015, try this in a recent version of Chrome: https://jsfiddle.net/xc9a74cs/ (Firefox *still* doesn't properly support the `name` property on functions). – T.J. Crowder Sep 18 '16 at 05:59
  • @T.J.Crowder Very nifty. – user2864740 Sep 18 '16 at 06:31

1 Answers1

3

As Rajaprabhu suggested in the comments, you can use:

helloWorldFunctions['hello.world']()
Kevin Lee
  • 1,370
  • 14
  • 22