4

I would like to have a function, which would be accessible to call it by other functions and should self execute when declared.

Like:

function some(var_one, var_two)
{
    // do something
}(var_one, var_two); // declare and execute (doesn't work)

function add_pair()
{
    // do something
    some(James, Amber);
}

What is a proper syntax to do this?

6
  • 2
    Can't you just call the function right after declaring it? As in, some(thing1, thing2); Commented Nov 1, 2012 at 14:35
  • @RoryMcCrossan - Assuming the pair of parentheses after the function declaration are intended to invoke the function, the syntax is not fine. Those parentheses will do nothing at all, since you can't invoke a declaration. Commented Nov 1, 2012 at 14:37
  • @JamesAllardice yes, you're correct, OP needs parentesis around the function declaration. Commented Nov 1, 2012 at 14:38
  • @James Allardice it's too trivial Commented Nov 1, 2012 at 14:47
  • @Rory McCrossan it would become an anonymous function with no ability to call it outside. Commented Nov 1, 2012 at 14:48

2 Answers 2

5

You cannot invoke a function declaration. The pair of parentheses following the declaration will do nothing at all. You could do something like this:

var some;
(some = function (var_one, var_two) {
    console.log(var_one, var_two);
})("a", "b");

​some("b", "c");​​​​

The above example will log "a, b", followed by "b, c". But that looks a bit messy. You could just stick with what you have and call the function like normal:

function some(var_one, var_two) {
    console.log(var_one, var_two);
}
some("a", "b");

Update

Note that by changing the function declaration to an expression (which is what happens in my first example above) you remove the ability to call the function before it appears to be declared in the source, since declarations are hoisted to the top of the scope in which they appear, and assignments happen in place. With a function declaration, the following example works perfectly:

some("a", "b"); // Call it before it appears to be declared
function some(var_one, var_two) {
    console.log(var_one, var_two);
}

I really can't see any reason why you would need to combine the declaration and invocation into one.

Sign up to request clarification or add additional context in comments.

2 Comments

nice example, but "var some" is one more line, like a usual function call.
@James - If you care that much about the extra line, just remove it and have some leak to the global scope! Other than that, I don't think you're going to shorten it.
0

No, you can't declare and execute the same time (unless in IE, creating two different functions). Have a look at http://kangax.github.com/nfe/

If your function has no return value, you could exploit that and let the function return itself, making it an immediately-executed-named-function-expression which is assigned to a variable:

var someGlobal = (function some(args) {
    // do something
    return some;
})(now_args);

// then
someGlobal(James, Amber);

Better use a declaration and an invocation, to make things clearer.

7 Comments

If you have to return some itself then the function won't be able to return whatever it's actually intended to return (if it is intended to return something). So "better use a declaration and an invocation" is probably a better suggestion.
Yes, I already wrote "if the function had no return value". Your assigned-and-executed function expression looks much better :-)
"undefined is not a function"
@Bergi - I wouldn't say "better", it's a mess :) I can see no reason you would ever need to do this over a simple declaration followed by a simple invocation. Sorry, didn't notice that you had already mentioned the return value part.
@James: Huh? No call to something undefined. Worksforme
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.