4

Wikipedia's article on first-class citizens states that "some authors" believe functions are only first-class citizens in a language if the language supports their creation at run-time. This article written by James Coglan plainly calls functions first-class citizens - whether or not he is aware of the dispute over the criteria for first-class, I do not know.

Here are my questions:

  1. Using the additional criteria of "creation at run-time", are JavaScript procedures first-class citizens?

It is worth mentioning that based upon more generalized criteria (applicable to other objects at-large), JavaScript functions are very obviously first-class citizens, namely they can be passed around as variables; therefore, I feel the criteria mentioned above adds an interesting dynamic - or, at least, a clarifying dynamic - to the conversation that is not - as one user writes - "arbitrary".

  1. If so, what does the creation of a function at run-time look like in JavaScript (is this what we call promises, callbacks, anonymous, etc.)?
  2. If not, what does the creation of a function look like at run-time in another language?
Thomas
  • 6,291
  • 6
  • 40
  • 69
  • This [SO question](http://stackoverflow.com/questions/20129236/creating-functions-dynamically-in-js) is pretty similar to your question. – AWolf Feb 11 '15 at 21:06
  • @ColeJohnson that's actually not technically correct. Have a read about the dispute over first-class citizen functions: http://programmers.stackexchange.com/questions/39742/when-is-a-feature-considered-a-first-class-citizen-in-a-programming-language-p (for example; there's a wealth more on the internet) – Thomas Feb 12 '15 at 02:32

2 Answers2

14

Functions can be created dynamically using the Function constructor

var adder = new Function('a', 'b', 'return a + b');

adder(3, 4); // returns 7

More elaborately, this could be used to apply an arbitrary binary operator:

function make_binary_fun(operator) {
    return new Function('a', 'b', 'return a ' + operator ' b');
}
var adder = make_binary_fun('+');
var multiplier = make_binary_fun('*');
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    True, but rarely used in real life; the example in Samuel Edwin Ward's answer is better because it demonstrates that functions are first class via ordinary language constructs used in everyday JavaScript, rather than through a construct that is rarely used and fairly evil (for the same reasons that `eval` is). – Mark Amery Feb 11 '15 at 21:11
  • 1
    The question isn't totally clear about what it means. That answer is making new closures of the same function, not new functions. I don't think the question was about what's used in real life, but just a general question about the language design and capabilities. – Barmar Feb 11 '15 at 21:12
  • 1
    It's currying, and technically is making a new function every time you call it. Not sure why that makes it not a valid answer as well. (I upvoted both btw) – Marc Feb 11 '15 at 21:16
  • Calling it a "new closure of the same function" doesn't match any terminology I'm familiar with from the JavaScript world. In JS terminology, each thing returned from Samuel's `makeIncrementer` is a `Function` (the returned objects have `Function` as their `.constructor` property) and they are non-equal. I don't see any reasonable perspective from which Samuel's answer would be *less* valid than this one. – Mark Amery Feb 11 '15 at 21:18
  • See my updated answer for the kind of thing that would be difficult to do with just a closure. – Barmar Feb 11 '15 at 21:19
  • 1
    I'm not sure JavaScript really has a distinction between a "new closure of the same function" and a "new function". As far as I know they're all just distinct instances of Function. – Samuel Edwin Ward Feb 11 '15 at 21:19
  • 2
    @Barmar If you *really* wanted to do your second example the closure way, you'd just use an `eval()` call inside the function expression. And if we're going to talk about the limitations of the two approaches, the `Function` constructor way doesn't permit you any access at all to the local variables of the function from which the `new Function` is constructed, which renders this method strictly less powerful than the closure method and makes many useful things impossible with it. – Mark Amery Feb 11 '15 at 21:24
  • I am accepting this answer *because* of the addition of the second example. – Thomas Feb 11 '15 at 22:44
  • 1
    Like I said in comments, it really depends on what problem you're trying to solve. It's not even clear what the point of the question is, it seems just about whether it matches some arbitrary definition, it doesn't affect actual programming. – Barmar Feb 11 '15 at 22:45
  • @Barmar the point of the question is whether or not JavaScript provides the ability to create functions at run-time, which you have answered. How that does **not** affect programming, I am unsure; however, the question informs a meta-conversation surrounding whether or not JavaScript is a functional programming language at its core. – Thomas Feb 11 '15 at 22:50
10

Here's an example of a function that creates a function at runtime in JavaScript:

function makeIncrementer(value) {
    return function(x) {
        return x+value;
    }
}

It takes a value, and returns a function that adds that value to its input.

Here are some examples of ways to call it:

var f = makeIncrementer(5);
f(2); // 7
f.call(null, 3); // 8
f.apply(null, [4]); /// 9
var object = {};
object.increment = f;
object.increment(5); // 10
Samuel Edwin Ward
  • 6,526
  • 3
  • 34
  • 62
  • @SamuelEdwinWard just want to recognize "out loud" yours as the first answer, as well as the answer that best fit the question, at first. Honestly, the choice between yours and the other was arbitrary. Thank you for the time you put into the answer. – Thomas Feb 12 '15 at 00:16