0

How to pass properties to a variable function? variable() works but when I try to pass a property i get "Uncaught TypeError: variable is not a function"

function firstFunc(a) {
  return `${a}`;
}

let variable = firstFunc();
document.write(variable(1));

2
  • 1
    I have no idea what you are trying to achieve here. newFunc is undefined. Commented Jul 7, 2016 at 17:50
  • These are not "properties", they are "parameters" or "arguments". There is no such thing as a "variable function", but a variable may hold a function. Also, can you please fix your post to use consistent names. Commented Jul 7, 2016 at 18:07

2 Answers 2

4

You wrote let variable = firstFunc(), so variable is the result of the execution of firstFunc().

You want instead that variable is a reference to the function (like an alias), so you need to do not put the brackets

function firstFunc(a) {
  return `${a}`;
}

let variable = firstFunc;
document.write(variable(1));

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

Comments

3

Two mistakes:

  • You confused firstFunc and newFunc
  • To assign a reference to the function to a variable, dont call it firstFunc(), just assign it without parenthesis ()

This works fine in browsers that support the ES6 features you are using:

function firstFunc(a) {
  return `${a}`;
}

let variable = firstFunc;
document.write(variable(1));

Comments

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.