Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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!

share|improve this question
10  
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? –  Crazy Eddie May 24 '12 at 16:11
4  
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
1  
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
show 2 more comments

4 Answers 4

up vote 44 down vote accepted

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>".

share|improve this answer
3  
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
show 1 more comment

This is a function:

function foo() {
    ...
}

This is a constructor:

function Foo() {
    ...
}
var f = new Foo();

This is a method:

Foo.prototype = {
    foo: function () { //this one
        ...
    }
}

This is a static method:
really it's more of a pseudo-static syntax for people accustomed to languages such as Java and C#

function Foo() {}
Foo.foo = function () { //this one
    ...
}

This is a private function:

function Foo() {
    function foo() {//this one
        ...
    }
    this.bar = function () {
        foo();
    }
}

This is an anonymous function:

function () {
    ...
}

This is an Immediately Invoked Function Experssion:

(function () {
    ...
}());

This is a recursive function:

function foo() {
    ...
    foo();
    ...
}

This is a function object:

var foo = new Function("...");
share|improve this answer
1  
Why do you call it a self-invoking anonymous function, if it never invokes itself? –  Tacroy May 25 '12 at 3:03
    
@Tacroy, function(){} is a function, function(){}() is a function that is considered to be "self-invoking". Typically these are used as closures or in a module pattern. –  zzzzBov May 25 '12 at 4:26
    
To the downvoters, anyone care to comment which part of this question is egregiously sloppy, shows no effort, or is clearly and perhaps dangerously incorrect? –  zzzzBov May 25 '12 at 4:28
    
Well for one thing you never actually answered the question :) What should he call functions in the global scope? –  Tacroy May 25 '12 at 5:09
    
@Tacroy, I typically start reading posts from the top, maybe you should do the same. The first line reads "This is a function", which is what you call a function in the global scope. You could call it a global function if it's attached to the global object (window in web browsers) or a local function if it's within another function. –  zzzzBov May 25 '12 at 13:10
add comment

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."

share|improve this answer
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
add comment

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."

share|improve this answer
3  
Wow, great minds think alike... –  Daniel Pryden May 24 '12 at 16:11
2  
Beat me to it. It even has a Wikipedia article. en.wikipedia.org/wiki/Non-recursive_function –  StriplingWarrior May 24 '12 at 16:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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