-1

It is really confusing to me that you can call a function in so many different ways. Let me explain.

When you start a project or work on already existing one you want your code to be of the highest quality so other developers may work on it with ease in the future. However there are so many different ways to create a project and you if you're not experienced you may make a big mess.

Let me show you some examples of functions and how you can call them, to show you what I mean.

Example 1: You can just declare a normal function and then call it by name:

function exampleFunc1(){
    console.log("hello 1");
}

exampleFunc1(); //hello 1

Example 2: You can also create function expression inside an object:

var mysteriousObject1 = {
   example2: function(){
       console.log("hello 2");
   }
}
mysteriousObject1.example2(); //hello 2

Example 3: or a function declaration inside a function:

function mysteriousObject2() {
    function decl() {
        console.log("hello 3");
    }
    decl();
}
mysteriousObject2(); //hello 3

Example 4: You can also call it this way as an anonymous function:

var example4 = function() {
    console.log("hello 4");
};
example4(); //hello 4

Example 5: Or even this way:

function exampleFunc5() { 
    function example5() {
        console.log("hello 5");
    }
    return {
        example5: example5
    };
}
var call = exampleFunc5();
call.example5(); //hello 5

Example 6: You can also create a class and call a function this way

class User {
  constructor(name) {
    this.name = name;
  }
  helloWorld() {
    console.log('hello ' + this.name);
  }
}

let user = new User("JavaScript");
user.helloWorld(); //hello JavaScript

There are just too many ways and I don't know which approach should I use.

  • I generally use already existing approach in projects I work on, but which one is the best?

    • How to write code to make it easily accessible for other developers in the future?

Thanks for answers.

Kacper Cieluch
  • 429
  • 3
  • 14
  • 1
    Use the approach you are most comfortable with, and works best with your project. I prefer Example 6 – Get Off My Lawn Jan 25 '18 at 16:09
  • 1
    You forgot (or don't know) quite a few more ways to define functions. But ultimately, there is no good reason to stick to a single pattern (although there are reasons to *avoid* some). Just be consistent. – Touffy Jan 25 '18 at 16:24
  • `mysteriousObject2` is not an object – Bergi Jan 25 '18 at 16:25
  • I prefer example 1. It's simple, and predictable. And using it with `.call()` works 100% of the time. – zer00ne Jan 25 '18 at 16:25
  • I don't understand why I got downvoted. It's a serious and really important question for me.. – Kacper Cieluch Jan 28 '18 at 10:36

1 Answers1

2
  • Prefer Example 1 (function declaration) over Example 4 (var with function expression). Declarations are simple, readable, and are hoisted for free.
  • Use Example 2 (methods in an object literal) if you need to put multiple functions in a structure
  • Use Example 5 (factory) or Example 6 (class) if you need to create multiple objects with different states/data that work alike.
Bergi
  • 513,640
  • 108
  • 821
  • 1,164