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));
Alexey Tseitlin
  • 1,031
  • 4
  • 14
  • 33
  • 1
    I have no idea what you are trying to achieve here. `newFunc` is undefined. – Quentin Jul 07 '16 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. –  Jul 07 '16 at 18:07

2 Answers2

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));
rpadovani
  • 7,101
  • 2
  • 31
  • 50
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));
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94