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

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the detailed answer! Do you know of any resources that speak more about this?
@Jessica add to answr

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.