When we write about a JavaScript function named 'f', we often refer to it as f(). We might write in an email, "You should call the function f() at this point". By choosing that way of writing we want to make it clear we are talking about a function, not about an "ordinary variable", which in JavaScript can hold any kind of object.
But, the notation "f ()" should not be used to refer to the function 'f'. Why? Because "f()" refers to the value returned by the function 'f' .
But doesn't this make it too easy to confuse 'f', which is a function, with variables named 'f', which may hold any type of object as well? Yes, it makes it more difficult.
But not really IF we take the viewpoint that a variable whose value never changes represents a function that takes 0 arguments, and will thus always "return" the same value - assuming we never change its value. And, it's good to use the convention that JavaScript itself uses. In JavaScript 'f()' means the value returned by the function 'f' when f is called.
According to functional programming a "function" is only a (pure) function if it always returns the same result for the same argument. Many JavaScript "functions" thus are not (real) (pure) functions. Variables which refer to values that are not JavaScript "functions" can be conceptually seen as "0-arity functions", if we never change their value after initial assignment. Or we should more properly say that the values stored in such variables are in fact 0 -arity functions.
I think the benefit of being able to distinguish between referring to a function f, and its value f(), is more important than the ability to indicate with a special form or syntax whether we are referring to a function or a variable holding some other type of value.
UPDATE: It needs to be emphasized that what I write about here is a convention for referring to JavaScript functions when writing about them. You can of course pick your own convention as you like. The way we define a function named 'f' in JavaScript is: "function f(){...}". Therefore it can be argued that a shorthand for that is "f()". I have used that convention myself in the past. But now I think I've seen the light: "f" stands for the function, "f()" for its result.
To emphasize that this is about how to write about JavaScript functions, I also changed the title of this post and dropped "... in JavaScript" from its end. This is not about "What f() means in JavaScript". That means what it means in JavaScript, the programming language. This post is about what f() means when we write about the JavaScript function 'f'.
Note the added expressive burden I face when writing this blog-post. I must not only make it clear whether I am referring to a function or its application. I also need to make clear whether I'm talking about JavaScript code, or writing about JavaScript (or even writing about writing about) code in a blog-post like this for instance. I hope I was able to be at least somewhat understandable!
© 2014 Panu Viljamaa. This work is licensed under a Creative Commons Attribution 4.0 International License
No comments:
Post a Comment