views:

39

answers:

3

My guess is that this is not possible, but I'd like f and f() to do the same thing.

   var f = function(str){ console.log(str||'foo'); }();

   f;                      // wanted output: 'foo'
   f();                    // wanted output: 'foo'
   f('bar');               // wanted output: 'bar'

Because f is no longer a function definition, it doesn't seem possible to do f(), but maybe I'm missing something. Any suggestions?

+1  A: 

Not possible. The parentheses are how javascript knows that you want the method object to be executed.

Adam Crossland
Yeah, I'd just like to make cleaner JavaScript when using action words. Ex: `Object.exec` - exec is a present-tense verb, so I expect it to be a function, thus `()` would just be added non-sense, but optional arguments might be necessary `Object.exec('in-background')`. I don't use verbs as variables unless they're past-tense, which would store booleans.
vol7ron
@vol7ron: I understand what you want to do, but unless the langauage specifically supports that syntax -- like Ruby does -- you're out of luck. And in JavaScript's case, you are out of luck.
Adam Crossland
Yeah, it's probably been asked before, I'll either have to use just the function literal, another variable, or use a global variable instead of parameters.
vol7ron
A: 

Remove the () from your def.

var f = function(str){ console.log(str||'foo'); };

Except your first case, all of them will work. You need to specify () to tell javascript to execute the function

Teja Kantamneni
The whole point of the question is to have both `f` and `f()`, dropping `()` would defeat the purpose :(
vol7ron
@vol7ron when you had `()` in your function definition means you are actually executing it and assigning the result which in this case is `f`.
Teja Kantamneni
`Because f is no longer a function definition, it doesn't seem possible to do f()` - I thought I already pointed that out.
vol7ron
+2  A: 

No that is not possible. The parentheses are required to determine that the function should be called.

The value of the expression f() is the result of calling the function, while the value of f is the function itself (and f.tostring() if you display it).

Guffa
Exactly, and I'd like to overload it :)
vol7ron
@vol7ron: That wouldn't work. If `f` was the same as `f()`, then `f('bar')` would be the same as `f()('bar')`, which is the same as `f()()('bar')`, which is the same as `f()()()('bar')`... and so on, making it impossible to use parameters in a function call.
Guffa
Yeah, it seems everyone wants to describe what's happening - I understand what is happening, that's why I posed the question. Really I'm curious if the language has evolved and if this is now possible, like Ruby, as [Adam](http://stackoverflow.com/users/226476/adam-crossland) pointed out.
vol7ron
@vol7ron: I explained what's happening in the answer, in the last comment I tried to explain why it's not possible to use the language in the way that you suggest. To allow `f` to have the same meaning as `f()` you would have to introduce a new keyword like `delegateof f ` to express the current meaning of `f`. That would have the effect that code like `$('a').click(function(){...});` would have to be rewritten as `$('a').click(delegateof function(){...});`, so removing parentheses from function calls would require more code in other places, and break the majority of existing web pages.
Guffa
I suppose your `$()` is an implementation of the `document.getElementById` for Prototype or JQuery, neither of which do I care to use :) - I don't mind too much about other people's code breaking, scripts should be maintained, not just left to forget. Otherwise there would be a whole bunch more security vulnerabilities available. Furthermore, if you are using a JS package, then really it should just be maintained by one source, which would then pack and effectively fix all those 'broken' pages. Your point is valid though, +1
vol7ron
@vol7ron: Yes, code should of course be maintained, but it's not as simple as just updating the code. As it's a breaking change, old and new code is incompatible. As there would be old and new browsers out there, almost all existing scripts would have to exist in two different versions for the forseeable future. Almost every Javascript question here on SO would be obsolete, as it only covers the old syntax. Almost everything that you do in Javascript would have to be done in two versions, so instead of making it slightly simpler to write Javascript, it would become twice as much work. :)
Guffa
I would think it'd be pretty easy to patch an old browser to use a newer JS standard. Even if the old browser is no longer supported. It comes down to one message `user: update or lose out`.
vol7ron
@vol7ron: If it only was that easy... :) We have customers that still use IE 6 because of some business critical application that doesn't work in newer versions. If they can't even update application to work in recent versions of the browsers, do you think that they would rewrite the code for a new JS standard? :P
Guffa