Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Static function declaration and the normal function declaration in Javascript?

Tags:

There are many ways one can declare a function in javascript. One of the ways is declaring a class and a static function inside is as showed below.

class className {
 static fucntionName() {
 }
}

another way of is declaring is through the tradition javascript style as showed below.

function functionName() {
}

I would like to know the advantages/disadvantages of using either of the cases. Is there any specific use cases for the static methods, why declare a class(we know that in javascript there is no need to instantiate the class in order to access the static function). Why not just use the traditional way (the second case in the above example) of function declaration in all/any use case?

I would like to understand this is in detail.

like image 816
Rahul Avatar asked Aug 09 '17 14:08

Rahul


People also ask

What is difference between function declaration and function definition in JavaScript?

The main difference between a function expression and a function declaration is the function name, which can be omitted in function expressions to create anonymous functions. A function expression can be used as an IIFE (Immediately Invoked Function Expression) which runs as soon as it is defined.

What is static function declaration?

A static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.

What is a static function in JavaScript?

Static methods are often utility functions, such as functions to create or clone objects, whereas static properties are useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances.


1 Answers

I would like to know the advantages/disadvantages of using either of the cases.

apples and oranges. A class doesn't have to be exported and a module doesn't have to be a class. They are not the same thing, although the exported module can be a class.

Is there any specific use cases for the static methods

When you want a class to provide some functionality, but this aint' something that is specific to a single instance of that class. Like var c = Point.interpolate(a, b, .5);
The static interpolate function doesn't belong to any of the points it's operating on. It's more appropriate to define this function static on the class that provides this functionality (interpolating points).

we know that in javascript there is no need to instantiate the class in order to access the static function

That's the very definition of a static function that you don't have to instantiate the class to call it. That's not something JS specific.

Why not just use the traditional way (the second case in the above example) of function declaration in all/any use case

Because as I started, a class and a module ain't necessarily the same thing. And it is nicer/cleaner to write

class Foo{
    ...
    static fn(){...}
}

than

class Foo {
    ...
}

Foo.fn = function(){...};

Although in the end, both is just little more than

function Foo(){...}
Foo.fn = function(){};
// + Foo.prototype stuff

But the class syntax is cleaner. Simpler to read and to understand, even if you're just scanning over this piece of code.

like image 155
Thomas Avatar answered Oct 13 '22 18:10

Thomas