最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can Functions Be Created at Run-time in Javascript? - Stack Overflow

programmeradmin7浏览0评论

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?

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?
Share Improve this question edited Feb 12, 2015 at 2:42 Thomas asked Feb 11, 2015 at 21:01 ThomasThomas 6,6906 gold badges43 silver badges71 bronze badges 2
  • This SO question is pretty similar to your question. – AWolf Commented Feb 11, 2015 at 21:06
  • @ColeJohnson that's actually not technically correct. Have a read about the dispute over first-class citizen functions: programmers.stackexchange./questions/39742/… (for example; there's a wealth more on the internet) – Thomas Commented Feb 12, 2015 at 2:32
Add a ment  | 

2 Answers 2

Reset to default 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('*');

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
发布评论

评论列表(0)

  1. 暂无评论