0

I'm trying to return an object with a function inside. When I call it, I get an error:

Uncaught TypeError: config.something is not a function

What am I doing wrong, and how can I fix it?

JSFiddle

function config() {
  function something() {
    console.log('something');
  }
  return {
    something: something
  };
}

config.something();
Jessica
  • 9,379
  • 14
  • 65
  • 136

1 Answers1

3

Description

Since config is a function not an object you need to call/execute it, this then returns the object that you can call .something on.

Code for Function

function config() {
  function something() {
    console.log('something');
  }
  return {
    something: something
  };
}

config().something();

Code for Object

var config = {
  something: function() {
    console.log('something');
  }
};

config.something();

More resources:

Community
  • 1
  • 1
abc123
  • 17,855
  • 7
  • 52
  • 82