What's more important in JS: performance, or the understandability of the code?

Then we’re equally confused with each other :smile:

You started going on about pure functions when that has no relation to how they’re declared, at all.

function increaseCounter() {
    counter += 1;
};

An IIFE is a function expression - just like the ones you are complaining about.

See https://developer.mozilla.org/en/docs/web/JavaScript/Reference/Operators/function for a further explanation of function expressions such as IIFEs.

I think this threads usefulness may have been exhausted long ago, I vote for a lock :slight_smile:

1 Like

[quote=“felgall, post:42, topic:223000, full:true”]
An IIFE is a function expression - just like the ones you are complaining about.[/quote]

Please note that I am not complaining about function expressions. I have no problem with people using them at all.
What I have been saying is that I personally find that the different syntax of function declarations and function expressions has been beneficial to give an indication as to their intent.

This may also be a cry of “but what of the buggy whip makers!” on my part, for I’ve found substantial benefit from using both declarations and expressions to make my code more expressive. Having said that though, I fully recognise and understand that some coding conventions of today nail things down more tightly with the enforcement of only function expressions. Function declarations are on the way out, like the buggy-whip makers of old.

So no, I am not complaining about using function expressions. It has been the sole use of them at the expense of everything else that I have been attempting to delve in to instead.

Having got that off our chest, what do you think are the pros and cons between using a function expression to reach out and change local variables, versus using IIFE’s to return the desired type of function?

An example function expression reaching out to change things:

var removeEvent;
var addEvent = function (...) {
    ...
    if (...) {
        removeEvent = function (...) {};
    else if (...) {
        removeEvent = function (...) {};
    }
    ...
};

And an example IIFE to assign the appropriate function:

var removeEvent = (function () {
    if (...) {
        return function (...) {
            ...
        };
    } else if (...) {
        return function (...) {
            ...
        };
    }
}());

But we have the pros and cons of function expressions versus IIFE’s to work through :smile:

IIFEs are function expressions so what is it we need to compare between function expressions that happen to be self executing and those that are not self executing?

Wow, thank you for the education.

[quote=“felgall, post:46, topic:223000, full:true”]so what is it we need to compare between function expressions that happen to be self executing and those that are not self executing?
[/quote]

What we are comparing here is the appropriateness of function expressions reaching out to change variables external to the function, and IIFE’s to instead assign functions to those variables.

The addEvent and removeEvent functions seem to be a good example to work with here, as they have come up in recent discussions.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.