Dres
Dres

Reputation: 1509

Can't access inner function properly

Not sure if this is the right title but should be quick help.

Pretty much I keep on getting errors on testing a function because "TypeError: ParseThis.changeIt is not a function". Here's my code. What am I missing that causing this type error? Thanks!

const ParseThis = () => {
    const changeIt = string => string;

    return { changeIt: changeIt() }

}

Edit: More details!

Thanks for the help again

Upvotes: 0

Views: 46

Answers (5)

jo_va
jo_va

Reputation: 13983

When you return your object, maybe you wanted to return the function and not the result of the call:

return { changeIt: changeIt };

or this which is more concise:

return { changeIt };

According to how you are using the translate function, I think you should export it this way:

const Translator = {
  const translate = string => string;
};

if (module.exports) {
  module.exports = Translator;
}

or this way:

const Translator = () => {
  const translate = string => string;
  return { translate };
}

if (module.exports) {
  module.exports = Translator();
}

Upvotes: 1

Matt Way
Matt Way

Reputation: 33199

The problem is that you are exporting a function, and not the object containing the nop function. You need to add parenthesis to your dummy:

const Translator = () => {
  const translate = string => string;
  return { translate };
};

if (module.exports) {
  module.exports = Translator(); // add parenthesis here
}

Alternatively you could run the function you import, but I suspect that would be different from your real Translator api.

Upvotes: 0

Castro Roy
Castro Roy

Reputation: 7823

Your function is returning an object, so instead of

ParseThis.changeIt()

You should be doing something like

const a = ParseThis();
a.changeIt('some string');

But, note that even in your example, changeIt in the returning object is not a function.

Probably you are trying this

const ParseThis = () => {
  const changeIt = string => string;

  return { changeIt: changeIt};
}

Note that I've used { changeIt: changeIt}, setting changeIt to a reference of the inner function changeIt. And you are using { changeIt: changeIt()} setting changeIt to the value returned of the inner function changeIt. Those are two different operations.

Upvotes: 0

Christian Vincenzo Traina
Christian Vincenzo Traina

Reputation: 10464

Let's analyze your code.

Let's start from this:

const changeIt = string => string;

At this point, changeIt is a function that, given a parameter, it returns that a parameter.

Without an arrow function, if we should use the old classic named function, it would be like this:

function changeIt(parameter) {
  return parameter;
}

What happens if you call changeIt() with no parameter? In javascript, when you pass no parameters ot a function, it's like you are passing undefined. So the function will return undefined.

Then you have this line:

return { changeIt: changeIt() }

But as we have seen, changeIt() is equal to undefined. So your code is equivalent to:

return { changeIt: undefined }

Which is clearly not a function!

What you probably meant to do, is not returning the result of function invokation, but return the function itself. So, instead that assigning changeIt(), just assign changeIt:

return { changeIt: changeIt }

Notice that you repeated twice the word changeIt, so you can get rid of this repetition and just write:

return { changeIt }

Upvotes: 0

Paul
Paul

Reputation: 27483

Return the function instead of calling it.

const ParseThis = () => {
    const changeIt = string => string;

    return { changeIt };

}

In the original post, changeIt() is a call to changeIt with no first parameter. It returns the value undefined. To return a function instead of calling it, omit the parenthesis.

Upvotes: 0

Related Questions