JavaScript Four Patterns to Invoke a Function<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

JavaScript Four Patterns to Invoke a Function

Posted on: May 25, 2017

There are four ways to call a function in JavaScript. In this article, we will see them all and see the pros and cons of each of them.

Function invocation pattern This is when a function is not attached to anything, it's floating which in fact is attached to "window" when in a browser if this one is created not within an object. Here is two examples:

1var f1 = function () {
2 console.log("F1");
3};
4f1();
5window.f1();
6function f2() {
7 console.log("F2");
8}
9f2();
10window.f2();

However, if a function is not bound to an object but created withing a function, this one belong to the function. Still, it is not attached to anything.

1var myObject = {
2 function2: function () {
3 console.log("Function2");
4 var function2_1 = function () {
5 console.log("Function2_1");
6 };
7 function2_1();
8 },
9};
10myObject.function2();

Method invocation pattern Method invocation pattern is when a function is attached to an object that is not instantiated with the "new" keyword.

1var myObject = {
2 function2: function () {
3 console.log("Function2");
4 },
5};
6myObject.function2();

Constructor invocation pattern I already covered in previous article about prototype. Invoking with a constructor trigger the prototype chain for the created object. Constructor function are created with the keyword "new".

1var MyClass1 = function () {
2 this.function2 = function () {
3 return "F2";
4 };
5};
6
7MyClass1.prototype.function1 = function () {
8 return "F1";
9};
10
11var instance = new MyClass1();
12console.log(instance.function1());
13console.log(instance.function2());

As you can see, you can invoke method from the instance or the prototype.

Apply invocation pattern

The last pattern is to use a function to call a function! This is possible because every object has some default function attached to their prototype, like the one that interest us which is "apply".

The following example shows how to invoke with apply. Both call to the apply function uses the class' prototype which call the function and apply. The difference is that one pass the method which has a member assigned with this, while the second call an anonymous object. The importance is to have a "m1" member because function1 access it in its prototype.

The important detail about "apply" is the parameter. The first one is the pointer this, the others are the the function arguments.

1var MyClass1 = function () {
2 this.m1 = "Member Value";
3};
4
5MyClass1.prototype.function1 = function () {
6 return "From function1 here is the m1 value : " + this.m1;
7};
8
9var instance = new MyClass1();
10var objectOnTheFly = { m1: "on-the-fly" };
11console.log(MyClass1.prototype.function1.apply(MyClass1()));
12console.log(MyClass1.prototype.function1.apply(objectOnTheFly));

In this example, the this got assigned dynamically and the function uses the value by accessing with this. In this article we saw 4 different ways to invoke function in JavaScript.