I have a collection of normal functions and self-calling functions within a javascript file. In my comments i want to say something along the lines of "This script can contain both self-calling and XXX functions", where XXX is the non-self-calling functions.

"Static" springs to my mind, but feel that would be incorrect because of the use of static methods in PHP, which is something completely different.

Anyone any ideas? Thanks!

  • 12
    Does that comment tell the user anything they shouldn't already know? Why would you not be able to have functions that don't call themselves? – Edward Strange May 24 '12 at 16:11
  • 5
    You need to define what you mean by 'self-calling'. Are you referring to recursive functions or functions where you're calling members using the 'this' keyword. The ones that use 'this' would be considered instance methods where the top-level function would be considered a class (ie it needs to be instantiated using the 'new' keyword). Non-recursive functions and/or non-class/instance functions are just plain ol' functions. JavaScript isn't an OOP language so there's no need to attach helper functions to a class (which is what static methods basically are). – Evan Plaice May 24 '12 at 16:17
  • 1
    you have functions and self-executing [anonymous] functions. just call them that. both are in fact functions... one just self-executes. Assuming of course you are talking about (function() { }()); – rlemon May 24 '12 at 16:30
  • 2
    This is a javascript thing, and calling it a self-executing function is misleading; the functions never call themselves, they're executed once immediately after definition and never again. You do it that way because otherwise the stuff going on inside the function will pollute the global scope. – Tacroy May 24 '12 at 17:27
  • 2
    I believe you're referring to Immediately Invoked Function Expressions (not self-calling functions) benalman.com/news/2010/11/… – stinkycheeseman May 24 '12 at 20:59

First, the "self-calling functions" aren't actually self-calling. I know that's what people in the Javascript community call them, but it's really misleading; the functions never reference themselves, and in fact a lot of the time there's no way to call that particular function more than once. If they were actually self-calling functions, then recursive functions would have been the best way to name them.

What you really have are (usually) immediately executed lambdas whose results are stored in a variable in order to limit the scope of their internals. "IEL" isn't as catchy as "self-calling functions", so I guess that's why the real name never caught on.

The thing is though, that's entirely too low-level; it's an implementation detail that nobody cares about (it's like saying "here be for loops"). Generally, when you're using those immediate-execution functions, the reason why you're using them is because you're making some sort of a module, which needs its own namespace.

If that's the case, then instead of saying "self-executing functions", you should say "this script contains modules that do <stuff>". Otherwise, you should figure out what you're trying to do with the functions, and say that's what's in your script.

Now, the reason why you use modules in Javascript is because otherwise everything goes into the global scope. Those other functions you're writing, that aren't going to be inside the modules (or whatever you decide they are), are going to end up there. So use that - "this script file contains both modules that do <stuff> and global functions that do <more stuff>".

  • 5
    I'm tempted to start calling them "urgent lambdas" or "instant lambdas"... Less syllables than "self-calling functions", too! – Izkata May 24 '12 at 18:56
  • It sounds like an assignment with a lambda expression on the right hand. What am I missing? – Jim Dennis May 24 '12 at 20:39
  • @JimDennis The lambda expression is immediately executed, and only the results of the lambda are stored in the assigned variable. Here's an article on the technique but basically it boils down to the fact that in Javascript pretty much everything ends up in either the global scope or the local function's scope, so you fake a function to hold your local context while keeping it out of the global context. – Tacroy May 24 '12 at 21:15
  • @JimDennis Also, in JQuery world, the idiom is used by modules to guarantee they have access to a real undefined value - since in Javascript, that word can be overwritten with whatever you want. ((function (window, undefined) { .. module code .. }) (window)) – Izkata May 24 '12 at 21:34
  • 1
    I go with auto-invoked functions. There's also some Acronym IFE..something that I can never remember, so it's clearly not a very good acronym. : ) – Erik Reppen May 25 '12 at 1:15

Revised Answer

OK, I see what you mean. I wouldn't call those "self-calling functions" -- the first thing that comes to mind is "immediately invoked functions". "Module" would also work, but I'm not sure how readily it would be understood.

Rather than talking about "self-calling" and "non-self-calling" functions, I'd document the script by explaining what members it adds into the global namespace.

For example, if your immediately invoked functions set up some objects, describe those objects. Basically, describe what the world looks like after your script has run, not how your script gets it to that point.

Original Answer

Since "self-calling" usually means "recursive", I would just say: "This script can contain both recursive and non-recursive functions."

  • 4
    hah! almost exactly the same :) – Kevin Hsu May 24 '12 at 16:11
  • 11
    Unfortunately that's not actually what he's asking about :( It's this weird Javascript idiom that doesn't actually mean what it says. – Tacroy May 24 '12 at 17:47

Maybe it's the self-calling functions that should be called something different, such as "recursive". Perhaps something along the lines of "This script can contain both recursive and non-recursive functions."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.