-1

Lately, I've noticed javascript convention has been shifting. Now, wherever I look, I see other developers using javascript are wrapping their functions in parenthesis to make it private (or something like that I'm not sure, javascript is a secondary language for me). For example...

(function something()
    { // something happens here 
    })();

As stuff (especially additional functions) gets nested, this hurts code readability, and is really making javascript ugly and look more and more like the nightmares of programming in LISP(how it got it's nickname "Lots of Idiotic Single Parentheses). I don't care if I have to type out a full word (or even a couple words) to get the same effect. Is there a good alternative to encapsulating in parentheses to make a function private and get the other benefits of the parenthetical encapsulation?

Edit: Apparently I misunderstood the point of the outside surrounding parenthesis that I'm questioning. Which makes me more confused about their purpose, actually.

lilHar
  • 1,735
  • 3
  • 21
  • 35
  • 2
    Depending on the tools you are using, ES6 has a [nice solution](https://github.com/ModuleLoader/es6-module-loader/wiki/Brief-Overview-of-ES6-Module-syntax) for this (similar to modules in Node.js) – nem035 Sep 14 '15 at 17:56
  • 5
    No. The only time this is necessary is if you have an IIFE, and it's two extra parens. You could also just define the function and execute it later. – Dave Newton Sep 14 '15 at 17:57
  • 1
    *"Is there a good alternative to encapsulating in parentheses to make a function private and get the other benefits of the parenthetical encapsulation?"* - It's not the parenthesis that's doing encapsulation. It's the function that's doing that (via a closure). The first pair of parenthesis just makes the function an expression, and the last pair executes it. Hence the name IIFE (Immediately Invoked Function Expression). – Joseph Sep 14 '15 at 17:59
  • 1
    Off the top of my head, I can't think of a reason why you'd need to nest this syntax - the "module" would normally be a single scope, and within that you'd have named functions. – IMSoP Sep 14 '15 at 17:59
  • Those extra parens don't do anything, most of the time. – ssube Sep 14 '15 at 17:59
  • 1
    http://browserify.org/, https://webpack.github.io/ – azium Sep 14 '15 at 18:00
  • 1
    Also.. embrace the Lisp. It will give back the love you put in. – azium Sep 14 '15 at 18:01
  • 2
    If you don't like the parens, you could always use the horrible `!function() { }()` hack. –  Sep 14 '15 at 18:07

2 Answers2

1
  • You can define regular function and call it when you need it

Example

var myObject = {
    something: function() {
    },

    somethingElse: function() {
    }
};

myObject.something();
  • ES6/ES2015 has now modules support

    //------ lib.js ------
    export const sqrt = Math.sqrt;
    export function square(x) {
        return x * x;
    }
    
    export function diag(x, y) {
        return sqrt(square(x) + square(y));
    }
    
    //------ main.js ------
    import { square, diag } from 'lib';
    console.log(square(11)); // 121
    console.log(diag(4, 3)); // 5
    

You can read more about modules here

Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
1

No. (Delta ES6 modules, or other pre-packaging require-like frameworks.)

The only time this syntax is necessary is if you have an IIFE, and it's only two extra parens (to turn the function into an expression that can be evaluated, i.e., executed).

You could also just define the function and execute it later, e.g., avoid the IIFE altogether).

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Where am I missing an extra set of parems in my example, if you don't mind me asking? – lilHar Sep 14 '15 at 18:45
  • @liljoshu No, I'm saying it's only two extra parens, `()`, to make the function into an expression that can be executed. It's not really much like Lisp at all. Edited to clarify. – Dave Newton Sep 14 '15 at 19:05
  • huh, I thought it was two around it and two following it. So what's the other parems for? – lilHar Sep 14 '15 at 19:50
  • 1
    @liljoshu That's to execute the function, like any JS function. – Dave Newton Sep 14 '15 at 19:56