Jessica
Jessica

Reputation: 9830

Return function in object not working

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();

Upvotes: 0

Views: 100

Answers (1)

abc123
abc123

Reputation: 18753

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:

Upvotes: 3

Related Questions